访问多字段搜索与结果,因为你键入

访问多字段搜索与结果,因为你键入

问题描述:

早上好,所有,访问多字段搜索与结果,因为你键入

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

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

这是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 

这是否引发任何红旗,为什么我得到一个崩溃?

您只需要改变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. 
+0

这是完美的,但有一个问题! 当我在搜索后输入一个空格时(例如:“Smith”),它会立即将搜索空白并且我没有做任何事情,因为没有关闭搜索表单并重新启动搜索表单,会恢复我的人员列表。一种方法来制定一个例外,所以如果一个错误字符中的usertype类型,它不会崩溃它,或者是否有可能抛出一个消息框,消除搜索文本框,并重新设置焦点?或者也许有一个更好的方式来处理这样的错误? –

+0

嗯,我不明白为什么会发生这种情况,要调试这个,注释掉On Error Resume Next,你可以用Me.txtSearch启动_Change子。Text = Trim(Me.txtSearch.Text)'。 - 在我的子集中,除了设置'.SelStart'之外,我还设置了'.SelLength = 0',但我不确定这是否有所帮助。 @WillDutcher – Andre

+0

由于某种原因,我不认为它完全喜欢它。这里是我所有的相关代码: 'code' '这将每当用户点击时重置行,给一个空白字段使用,并重新搜索以显示所有人: Private Sub txtSearch_Click() Me.txtSearch.Text = “” Me.Requery 随着Me.txtSearch .SETFOCUS .SelStart = LEN(Me.txtSearch.Text) 结束随着 结束子 'code'

首先,文本框点击事件,由此当您单击框,它会清除文字和重置搜索(无需复位按钮)

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