使用vba访问搜索 - 在子表单中显示结果而不更改RecordSource

时间:2017-08-23 12:06:08

标签: database ms-access

如何在不将RecordSource更改为我的sql字符串的情况下显示关键字搜索的结果? 原因是,如果我更改RecordSource,数据表中的标准过滤器会将表名更改为子表单

好的,这是我的7个表中的SQL查询:

SELECT table1.*, table2.*, table3.*, table4.*, table5.*, table6.*, table7.* FROM table1 LEFT JOIN (((table2 LEFT JOIN table3 ON table2.ID = table3.ID) LEFT JOIN table7 ON table3.ID = table7.ID) LEFT JOIN ((table4 LEFT JOIN table5 ON table4.ID = table5.ID) LEFT JOIN table6 ON table5.ID = table6.ID) ON table7.ID = table4.ID) ON table1.ID = table2.test;

这是我的搜索按钮:

Private Sub btnSearch_Click()
Dim sql As String
Dim query As QueryDef

' sql query for search with keywords
sql = " SELECT * FROM qryData WHERE " & BuildFIlter


'set the new sql as RecordSource
Me.sfrmSearch.Form.RecordSource = sql


'Delete the query if it already exists
On Error Resume Next
DoCmd.DeleteObject acQuery, "qryExport"

'Set the query and save it in current database
Set query = CurrentDb.CreateQueryDef("qryExport", sql)
End Sub                                                                                                                                                         

Private Function BuildFIlter() As Variant
Dim varWhere As Variant

varWhere = Null 'Main Filter

' Check for LIKE test_Name
If Me.txtKeywords > "" Then
    varWhere = varWhere & " [test_name] LIKE '*" & Me.txtKeywords & "*' "
End If


' Check if there is a filter to return...
If IsNull(varWhere) Then
    varWhere = ""
End If 
BuildFIlter = varWhere 
End Function

现在是我的导出按钮:

Private Sub btnExport_Click()
Dim filterSQL As String
Dim sql As String
Dim query As QueryDef
Dim x As Integer

filterSQL = " SELECT * FROM qryData "

   If Me.sfrmSearch.Form.OrderBy <> vbNullString And Me.sfrmSearch.Form.OrderByOn = True And Me.sfrmSearch.Form.Filter <> vbNullString And Me.sfrmSearch.Form.FilterOn = True Then
     filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter & " ORDER BY " & Me.sfrmSearch.Form.OrderBy
        If Me.txtKeywords > "" Then
            filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter & " AND " & "(" & BuildFIlter & ")" & " ORDER BY " & Me.sfrmSearch.Form.OrderBy
        End If
    x = 1
    End If 

    If Me.sfrmSearch.Form.Filter <> vbNullString And Me.sfrmSearch.Form.FilterOn = True And Me.sfrmSearch.Form.OrderByOn = False Then
        filterSQL = filterSQL & " WHERE " & Me.sfrmSearch.Form.Filter
            If Me.txtKeywords > "" Then
              filterSQL = filterSQL & " AND " & "(" & BuildFIlter & ")"
            End If
        x = 1
        MsgBox (filterSQL)
    End If 

    If Me.sfrmSearch.Form.OrderBy <> vbNullString And Me.sfrmSearch.Form.OrderByOn = True And Me.sfrmSearch.Form.FilterOn = False Then
        If Me.txtKeywords > "" Then
            filterSQL = filterSQL & " WHERE " & BuildFIlter & " Order By " & Me.sfrmSearch.Form.OrderBy

        Else
            filterSQL = filterSQL & " Order By " & Me.sfrmSearch.Form.OrderBy
        End If
        x = 1
    End If 

    If x = 1 Then
        On Error Resume Next

        'Delete the query if it already exists
        DoCmd.DeleteObject acQuery, "qryExport"

        'Set the query and save it in current database
         Set query = CurrentDb.CreateQueryDef("qryExport", filterSQL)

     Else
       Call btnSearch_Click
    End If


DoCmd.OutputTo acOutputQuery, "qryExport", "Excel Workbook (*.xlsx)", , , , , acExportQualityPrint
 End Sub

我只能在一列中设置过滤器,所有其他值都不可用,如果我将其设置为一列并导出它,则访问请求参数值(访问将子表单名称作为表名而不是queryname作为表名,以便列不可用)

我必须将记录源设置为sql,因为我想在子表单中看到我的过滤数据drom关键字

1 个答案:

答案 0 :(得分:0)

您似乎正在使用大量VBA代码,并且您无法完全理解它正在做什么。我猜你自己没有写过。

您的问题的快速解决方法是替换此行:

varWhere = varWhere & " [test_name] LIKE '*" & Me.txtKeywords & "*' "

这一行:

varWhere = varWhere & Me.txtKeywords

这会导致您需要在WHERE框中输入有效的txtKeywords语句(例如Column1 LIKE "*searchTerm1*" AND Colum2 LIKE "*searchTerm2*"),但这样您可以更好地控制确切的过滤器你正在使用。

从长远来看,积极调整维护由其他人编写的数据库是一个糟糕的计划,而在编写内容时没有很好地理解她/他的意图,以及代码正在做什么。您可能想投资一些VBA课程。