Access Multi-field Search with Results as you type

后端 未结 2 1978
情话喂你
情话喂你 2021-01-24 22:08

Good morning, all,

My question today is regarding multi-field search.

I have a split form (fields and individual record at the top, all data in datasheet view on

相关标签:
2条回答
  • 2021-01-24 22:50

    First, the textbox click event whereby when you click the box, it clears the text and resets the search (no need for a reset button)

    Private Sub txtSearch_Click()
        Me.txtSearch.SetFocus 'new line of code
        Me.txtSearch.Text = ""
        Me.Requery
    With Me.txtSearch
        .SetFocus
        .SelStart
    End With
    End Sub
    

    This is the actual search, that will search multiple fields

    Private Sub txtSearch_Change()
        Dim strFilter As String
        Dim sSearch As String
        On Error Resume Next
    
    If Me.txtSearch.Text <> "" Then
        sSearch = "'*" & Replace(Me.txtSearch.Text, "'", "''") & "*'"
        strFilter = "[Last_Name] Like " & sSearch & " OR [First_Name] Like " & sSearch & " OR [SSN] Like " & sSearch
        Me.Filter = strFilter
        Me.FilterOn = True
    Else
        Me.Filter = ""
        Me.FilterOn = False
    End If
    
    If Me.Recordset.RecordCount = 0 Then 'new line of code
        Me.Filter = "" 'new line of code
        Me.FilterOn = False 'new line of code
        Me.txtSearch.SetFocus 'new line of code
        Me.txtSearch.Text = "" 'new line of code
    Exit Sub 'new line of code
    End If 'new line of code
    
    With Me.txtSearch
        .SetFocus
        .SelStart = Len(Me.txtSearch.Text)
    End With
    End Sub
    
    0 讨论(0)
  • 2021-01-24 23:06

    You only need to change the definition of strFilter. For convenience I would use an additional variable.

    Dim sSearch As String
    If Me.txtSearch.Text <> "" Then
        ' Avoid problems with search strings containing "'"
        sSearch = "'*" & Replace(Me.txtSearch.Text, "'", "''") & "*'"
        ' Add all fields you want to search with OR
        strFilter = "[Last_Name] Like " & sSearch  & " OR [First_Name] Like " & sSearch ' etc.
    
    0 讨论(0)
提交回复
热议问题