Access Multi-field Search with Results as you type

后端 未结 2 1977
情话喂你
情话喂你 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
    

提交回复
热议问题