访问:将表单结果转换为 table

Access: converting form result to table

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

在输入栏中输入关键词,点击"search"按钮显示结果。

搜索按钮具有以下代码:

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" 并按搜索按钮,它会显示州设置为纽约的结果。

我的问题是:如何将表单返回的结果导出到 table 并保存?如果我能做到这一点就好了。

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

DoCmd.RunSQL "SELECT * INTO MyTable FROM MasterData" & strWhere