vbams-accesssearch-box

dynamic search box filters using LIKE


I have a dynamic search box filtering a subform based on user input. I also have a few filter buttons that filter the same subform. I set up the search box to incorporate preexisting filters applied by those buttons.

All that works fine. The problem I have is:

Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'"

Would be nice if someone has some ideas how to go about these issues. Here the full code for my searchbox:

Private Sub SearchBoxStoffe_KeyUp(KeyCode As Integer, Shift As Integer)

On Error GoTo errHandler

Dim filterText As String

'Apply dynamic filter for current filter category.
If Len(SearchBoxStoffe.Text) > 0 Then
    filterText = SearchBoxStoffe.Text
    If Forms![HUB]![FilterAlleLink] = "" Then
        Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*'"
    Else
        Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'"
    End If
    Me.FilterOn = True
    'Retain filter text in search box after refreshing.
    SearchBoxStoffe.Text = filterText
    SearchBoxStoffe.SelStart = Len(SearchBoxStoffe.Text)
Else
    'Revert to current main filter category.
    If Forms![HUB]![FilterAlleLink] <> "" Then
        Call FilterStoffe("[kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'")
    Else
        If Forms![HUB]![FilterAlleLink] = "" Then
            Me.Filter = ""
            Me.FilterOn = False
        End If
    End If
End If

'Set focus back to search box
SearchBoxStoffe.SetFocus

Exit Sub

errHandler:

MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."

End Sub

Solution

  • There were 2 issues that prevented the searchbox from running as intended:

    1. Object references in the project were created with 2 different language versions of access. Objects would call other objects using the formulation of one language, which in turn called objects using referencing in another language etc. In cases where fields and/or queries would return empty, this would cause some of the references to no longer function as intended. The result was the program running out of stack space, empty controls on subforms that returned empty queries, objects not being found and more.
    2. The searchbox filter was lagging behind the text in the searchbox by one event. If entering a new search string, the applied filter would always be missing the last character when using SearchBoxStoffe in the filter statement. Entering "Wood" would cause the filter to apply "Woo" etc.

    The solutions are the following:

    1. Fix all the references in the file manually to either language version and do not mix them up going forward.
    2. The Value of the search box SearchBoxStoffe is not yet updated on either the KeyUp or the Change event when entering a new character. This can be fixed by substituting the Text value instead, which is updated already. Simply change line 11 to Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*'" and line 13 to Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'". The info originally came from @KostasK in his solution:

    Keep in mind, within the Change() event, the Text property gets updated with every keystroke and when the control loses the focus, it gets copied to the Value property.

    Which works too btw, just wasn't able to be verified since issue 1 prevented the code from running correctly. Answer by Kostas K.