我正在创建一个按类型搜索的动态搜索,当用户在文本框中键入内容时,该搜索将过滤数据列表。
Private Sub TxtSearch_Change()
Dim CursorPosition As Long
Dim strSearch As String
Dim sqlSearch As String
CursorPosition = TxtSearch.SelStart
Me.Dirty = False 'set the dirty property to false to save the current value
strSearch = ""
If Not IsNull(Me.TxtSearch.Value) Then
strSearch = Me.TxtSearch.Value
End If
searchLength = Len(strSearch)
If searchLength < CursorPosition Then
For i = 1 To (CursorPosition- searchLength)
strSearch = strSearch + " "
Next
End If
'Check if a keyword has been entered or not
If strSearch = "" Then
Me.TxtSearch.SetFocus
sqlShowAll = "SELECT * FROM qrySearch"
Forms![frmSearch]!fsubTest.Form.RecordSource = sqlShowAll
Else
sqlSelect = "SELECT * FROM qrySearch WHERE ("
sqlLastName = "(LastName Like ""*" & strSearch & "*"")"
sqlFirstName = " OR (FirstName Like ""*" & strSearch & "*"")"
sqlFullName = " OR (FullName Like ""*" & strSearch & "*"")"
sqlEnd = ")"
sqlAllNames = sqlLastName & sqlFirstName & sqlFullName
sqlSearch = sqlSelect & sqlAllNames & sqlEnd
Forms![frmSearch]!fsubTest.Form.RecordSource = sqlSearch
End If
TxtSearch.SelStart = CursorPosition
End Sub
访问权限会截断文本字段中的尾随空格。有办法解决这个问题吗?我已经实现了for循环来恢复尾随空格以进行搜索,但是我想保存尾随空格,以便在用户继续键入时空格不会消失。例如,我可以输入“ Jane”并搜索“ Jane”,但是当返回到文本框时,我会看到“ Jane”,所以我永远不能键入“ Jane Doe”,而只能键入“ JaneDoe”。
答案 0 :(得分:1)
这是我用来完成您正在寻找的代码。我在其中输入搜索框“ Searchfor”,并在其中包含数据的组合框中搜索“ SearchResults”。还有一个文本框“ SrchText”,由查询“ QRY_SearchAll”使用。对于要在组合框中显示的每个字段,该查询都是一系列的“像“ ”&[Forms]![FRM_SearchMulti]![SrchText]&“ ””,请参见图片。
Private Sub SearchFor_Change()
'Create a string (text) variable
Dim vSearchString As String
'Populate the string variable with the text entered in the Text Box SearchFor
vSearchString = SearchFor.Text
'Pass the value contained in the string variable to the hidden text box SrchText,
'that is used as the sear4ch criteria for the Query QRY_SearchAll
SrchText = vSearchString
'Requery the List Box to show the latest results for the text entered in Text Box SearchFor
Me.SearchResults.Requery
'Tests for a trailing space and exits the sub routine at this point
'so as to preserve the trailing space, which would be lost if focus was shifted from Text Box SearchFor
If Len(Me.SrchText) <> 0 And InStr(Len(SrchText), SrchText, " ", vbTextCompare) Then
'Set the focus on the first item in the list box
Me.SearchResults = Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of the List Box
DoCmd.Requery
'Returns the cursor to the the end of the text in Text Box SearchFor,
'and restores trailing space lost when focus is shifted to the list box
Me.SearchFor = vSearchString
Me.SearchFor.SetFocus
Me.SearchFor.SelStart = Me.SearchFor.SelLength
Exit Sub
End If
'Set the focus on the first item in the list box
Me.SearchResults = Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of the List Box
DoCmd.Requery
'Returns the cursor to the the end of the text in Text Box SearchFor
Me.SearchFor.SetFocus
If Not IsNull(Len(Me.SearchFor)) Then
Me.SearchFor.SelStart = Len(Me.SearchFor)
End If
End Sub
有关此系统的一个警告:它使用重新查询而不是刷新。对于合理快速的系统上的合理数量的记录而言,这很好。我发现,当我尝试在古老的Sharepoint服务器上使用相同的代码存储数据时,键入的每个字母后都会出现10秒的延迟。因此,如果您要处理大量记录或服务器速度较慢,则可能需要将“ requery”更改为“ refresh”。