在您键入时访问带有结果的多字段搜索

时间:2017-10-18 12:33:27

标签: vba ms-access search access-vba

早上好,全部,

我今天的问题是关于多字段搜索。

我有一个拆分表单(顶部的字段和单个记录,底部的数据表视图中的所有数据)。我有多个要搜索的字段,以便用户可以根据多个条件找到特定的人。

以下是一位同事帮助我的VBA,它现在完美地工作,并且我想通过允许它搜索超过的更多功能来添加它只是一个领域。

Private Sub txtSearch_Change()
    Dim strFilter As String
    On Error Resume Next
    If Me.txtSearch.Text <> "" Then
        strFilter = "[Last_Name] Like '*" & Me.txtSearch.Text & "*'"
        Me.Filter = strFilter
        Me.FilterOn = True
    Else
        Me.Filter = ""
        Me.FilterOn = False
    End If
    With Me.txtSearch
        .SetFocus
        .SelStart = Len(Me.txtSearch.Text)
    End With
End Sub

每次输入字母时,搜索都会重新查询并提供符合该搜索条件的信息。我想要它做的是过滤,即使它是First_Name字段或SSN字段等。任何人都可以提供我需要使用的代码吗?我一直在搜索多个论坛,视频,帖子等,似乎没有任何差别,因为我一直在抛出错误。

我需要什么样的OR语句才能使我的搜索跨越表单中的多个字段(假设有一个)?

*请注意,我想在没有搜索按钮的情况下执行此操作,因此我希望将其保留为Change()事件, AfterUpdate()。

谢谢!

修改

标准回复评论的代码太长了:

此代码挂起。我可能错误地设置了VBA。

'This code works great, but if I put in a space character, it crashes the DB
Private Sub txtSearch_Change()
    Me.txtSearch.Text = Trim(Me.txtSearch.Text)
    Dim strFilter As String
    Dim sSearch As String

    If Me.txtSearch.Text <> "" Then
        sSearch = "'*" & Replace(Me.txtSearch.Text"'", "''") & "*'"
        strFilter2 = "[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
    With Me.txtSearch
        .SetFocus
        .SelLength = 0
        .SelStart = Len(Me.txtSearch.Text)
    End With
End Sub


'This code is what I have that will reset the textbox to blank and requery, giving me all the people in the DB
Private Sub txtSearch_Click()
    Me.txtSearch.Text = ""
    Me.Requery
    With Me.txtSearch
        .SetFocus
        .SelStart = Len(Me.txtSearch.Text)
    End With
End Sub

这是否会引发任何危险信号,说明我崩溃的原因?

2 个答案:

答案 0 :(得分:1)

您只需要更改strFilter的定义。为方便起见,我会使用一个额外的变量。

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.

答案 1 :(得分:1)

首先,文本框点击事件,当您单击该框时,它会清除文本并重置搜索(不需要重置按钮)

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

这是实际搜索,将搜索多个字段

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