多字段搜索,在您键入时显示结果 [英] Multi-Field Searching that show results as you type
问题描述
我的一个同事回答了我的问题,但是我想在这里重新发帖,以防其他人遇到问题,并提出问题,您将在最后指出.原始帖子(如果有兴趣的话)在这里:
I had gotten my question answered by a colleague of mine, but I wanted to re-post here in case anyone else has the issue, and to pose the question, which you'll note at the end. The original post, if interested, is here: Original Post
非常感谢社区中的所有人,特别是@Andre的帮助!
Many thanks to all in the community here, particularly @Andre for your help!
真奇怪,但是当我这样做的时候,它一直试图把我所有的字母都放回去.示例:如果我搜索"Smith",它将输入为"htims".我敢肯定它与点击事件有关,但是我有一个朋友看着它,我想她破解了它!看起来像这样(我在新代码行中添加了注释):
It was strange, but when I did that, it kept trying to put in all my letters backwards. Example: If I searched "Smith", it would enter it as "htims". I'm sure it had something to do with the on click event, but I had a friend look at it and I think she cracked it! It looks like this (I've added a comment to what the new line(s) of code is/are):
首先,文本框单击事件,当您单击该框时,它将清除文本并重置搜索(无需重置按钮)
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
这似乎效果很好.在我认为此问题已经解决的同时,我确实有一个问题,如果您能帮助我确定一下,为什么当我将.Text替换为.Value时,这些字母为什么会倒退?
This seems to be working great. And while I'm considering this issue complete, I do have a question, if you could help me identify, why were the letters going in backwards when I replaced the .Text with a .Value?
非常感谢大家的帮助!
推荐答案
首先,您需要将Click
事件更改为Enter
并取消.SetFocus
,因为控件将具有焦点:
First you need to change the Click
event to Enter
and scrap the .SetFocus
since the control will have the focus:
Private Sub txtSearch_Enter()
With Me
.txtSearch.Value = ""
.Requery
End With
End Sub
关于.Text
和.Value
之间的区别:
.Text
属性会随着您进行的每次击键而更新,而当控件失去焦点时,.Value
属性将仅更新.
The .Text
property updates with every keystroke you make where the .Value
property gets updated only when the control loses focus.
在Change
事件上,您应该检查两件事:a)文本框具有值(.Text)来应用过滤器,b)如果没有返回记录,则用recordcount清除过滤器.
On the Change
event, you should check two things: a) the textbox has a value (.Text) to apply the filter and b) the recordcount to clear the filter if no records returned.
Private Sub txtSearch_Change()
Dim strFilter As String, sSearch As String
On Error Resume Next
With me
If .txtSearch.Text <> "" Then
sSearch = "'*" & Replace(.txtSearch.Text, "'", "''") & "*'"
strFilter = "[Last_Name] Like " & sSearch & " OR [First_Name] Like " & sSearch & " OR [SSN] Like " & sSearch
.Filter = strFilter
.FilterOn = True
End If
If .Recordset.RecordCount = 0 Then
.Filter = ""
.FilterOn = False
End If
End With
End Sub
字母向后移动的原因是.SelStart
属性,每次键入字母时,光标都会在开头处跳动,从而给您留下了将单词向后翻转的印象.
The reason for the backward letters, was the .SelStart
property where the cursor was jumping at the beginning every time you typed a letter, thus giving you the impression it was flipping the word backwards.
这篇关于多字段搜索,在您键入时显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!