0

我创建了一个基本的搜索表单,其中包含输入字段、按钮和显示结果的区域。

在输入字段中输入关键字,然后单击“搜索”按钮显示结果。

搜索按钮有以下代码:

Private Sub mySearchQuery_Click()
Dim strSelect As String
Dim strWhere As String

If Len(Trim(Me!searchFirst.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND First_Name Like ""*" & Me!searchFirst.Value & "*"""
End If 
If Len(Trim(Me!searchLast.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND Last_Name Like ""*" & Me!searchLast.Value & "*"""
End If
If Len(Trim(Me!searchState.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND State Like ""*" & Me!searchState.Value & "*"""
End If
If Len(Trim(Me!searchLIC.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND LIC Like ""*" & Me!searchLIC.Value & "*"""
End If
If Len(Trim(Me!searchNPN.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND NPN Like ""*" & Me!searchNPN.Value & "*"""
End If
If Len(Trim(Me!searchEmail.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND Email Like ""*" & Me!searchEmail.Value & "*"""
End If
If Len(Trim(Me!searchDateFrom.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND EXP >= #" & Me.searchDateFrom.Value & "#"
End If
If Len(Trim(Me!searchDateTo.Value) & vbNullString) > 0 Then
    strWhere = strWhere & " AND EXP <= #" & Me.searchDateTo.Value & "#"
End If  

strSelect = "SELECT * FROM MasterData"
If Len(strWhere) > 0 Then
strSelect = strSelect & " WHERE " & Mid(strWhere, 6)
End If

Me.RecordSource = strSelect
' SubForm.Form.RecordSource = sql
End Sub

此搜索按钮将根据给定条件搜索项目。如果我在状态输入字段中输入“NY”并按搜索按钮,它将显示状态设置为 NY 的结果。

我的问题是:如何将返回的结果从表单导出到表格并保存?如果我能做到,那就太好了。

4

1 回答 1

0

只需使用简单的 SQL。没那么难。

DoCmd.RunSQL "SELECT * INTO MyTable FROM MasterData" & strWhere
于 2017-05-05T17:00:17.937 回答