【问题标题】:Creating a form to search for records based on multiple criteria创建表单以根据多个条件搜索记录
【发布时间】:2016-02-16 17:28:19
【问题描述】:

我正在尝试创建一个允许您根据多个条件返回结果的表单。

我有FirstName 字段、LastName 字段和State 字段。

我还有一个名为 searchFirstsearchLastsearchState 的文本框,用户可以在其中输入条件。

搜索按钮点击后会执行以下代码。

Private Sub mySearchQuery_Click()

Dim filter As String

Dim rtFirstName As String
Dim rtLastName As String
Dim rtState As String

rtFirstName = Me.searchFirst.Value
rtLastName = Me.searchLast.Value
rtState = Me.searchState.Value

If Not IsNull(rtFirstName) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(FirstName like""*" & rtFirstName & "*"")"
End If


If Not IsNull(rtLastName) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(LastName like""*" & rtLastName & "*"")"
End If

If Not IsNull(rtState) Then
        If Not IsNull(filter) Then filter = filter & " AND "
        filter = filter & "(State LIKE""*" & rtState & "*"")"
End If

' Now re-construct the SQL query '
Dim sql As String
sql = "SELECT * FROM MainData"

If Not IsNull(filter) Then
        sql = sql & " WHERE " & filter
End If

Me.RecordSource = sql
'SubForm.Form.RecordSource = sql

End Sub

我在下面收到以下错误。

运行时错误“3075”:查询中的语法错误(缺少运算符) 表达式 'AND (FirstName like"*tracy*") AND (lastName like"*Smith*") AND (State LIKE"*ga*")'。

我不确定为什么AND 会出现在搜索查询的开头?

【问题讨论】:

    标签: ms-access ms-access-2016


    【解决方案1】:

    我不确定为什么在搜索的开头包含 AND 查询?

    由于您有Dim filter As Stringfilter 永远不能包含 Null。这意味着这些 If 条件 ... If Not IsNull(filter) ... 将始终为 True。

    同样,Not IsNull(rtFirstName)Not IsNull(rtLastName)Not IsNull(rtState) 将始终为 True。

    最终结果是代码向您的filter 字符串添加了另一个条件片段,无论相应的搜索文本框是否包含任何内容,并且每个片段都以" AND " 为前缀。

    考虑到这些要点,您可以重构代码以仅在相应的搜索文本框中有内容时添加filter 段,并决定何时包含" AND "。但是,我发现为它们中的每一个都包含" AND ",然后在将其添加到WHERE 子句之前从filter 中删除第一个" AND " 会更简单。

    Private Sub mySearchQuery_Click()
        Dim strSelect As String
        Dim strWhere As String
    
        If Len(Trim(Me!searchFirst.Value) & vbNullString) > 0 Then
            strWhere = strWhere & " AND FirstName Like ""*" & Me!searchFirst.Value & "*"""
        End If
    
        If Len(Trim(Me!searchLast.Value) & vbNullString) > 0 Then
            strWhere = strWhere & " AND LastName 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
    
        ' Now re-construct the SQL query
        strSelect = "SELECT * FROM MainData"
    
        ' only add WHERE clause if we have something in strWhere    
        If Len(strWhere) > 0 Then
            ' use Mid() to ignore leading " AND "
            strSelect = strSelect & " WHERE " & Mid(strWhere, 6)
        End If
        Debug.Print strSelect ' <- inspect this in Immediate window; Ctrl+g will take you there
    
        ' enable one of these RecordSource lines after confirming Debug.Print shows you what you need
        'Me.RecordSource = sql
        'SubForm.Form.RecordSource = sql
    End Sub
    

    【讨论】:

    • 非常感谢!是的!按预期工作。你救了我 5 个小时的压力!谢谢谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多