在 Access 中截断尾随空格以进行“键入时搜索”

Truncating trailing spaces in Access for Search-As-You-Type

我正在创建一个动态的“键入时搜索”,它会在用户在文本框中键入时过滤数据列表。

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

Access 截断文本字段中的尾随 space。有办法解决这个问题吗?我已经实现了一个 for 循环来恢复尾随 space 以用于搜索目的,但我想保存尾随 space 以便在用户继续输入时 space 不会消失.例如,我可以输入 "Jane " 并搜索 "Jane " 但是当返回到文本框时,我会看到 "Jane" 所以我永远不能输入 "Jane Doe" 而只能输入 "JaneDoe".

这是我用来完成您要查找的内容的代码。我将搜索框 "Searchfor" 作为我键入的位置,并将 "SearchResults" 作为包含数据的组合框。还有一个文本框 "SrchText",查询 "QRY_SearchAll." 使用该查询是一系列 "Like "" & [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.'