I have a simple search form which uses a text box. In the OnChange event of the text box, I call cmdSearch_Click so that the form is automatically filtered as the user enters data. For an example they could type "1" in Me.txtSearchSerialNum and it will filter the form based on all serial numbers which have the number 1 in them. Then the user could type an extra 1 (so, "11") and it would further filter results. For some reason, it is not working as it should. If I type in "1" it calls cmdSearch_Click and then filters the form as if I have not typed in anything. I've created a msgBox for debugging and it will display nothing. If I were to add another 1 (so, "11"), then it will filter the form for 11. I get the same results no matter what I enter, it seems like it doesn't recognize when there's only one character maybe?
The reason I used the OnChange event and not after update is I want the form to automatically filter without the user having to click on a button or move the focus off of the tet box.
So I did some troubleshooting. In the OnChange event of the text box, I added msgBox(Me.txtSerialNum.text) and that displays 1. Then I added the same thing in the private sub cmdSearch_Click, and it displays nothing. So, I thought perhaps I needed to wait before calling cmdSearch from the OnChange event of the text box, so I added some code to wait 5 seconds. Still same results.
What am I doing wrong?
Here's my code:
Code:
Private Sub txtSearchSerialNum_Change() 'OnChange event is used on text boxes instead of AfterUpdate. MsgBox (Me.txtSearchSerialNum.Text) 'for debugging
Call cmdSearch_Click
Me.txtSearchSerialNum.SetFocus 'Sets focus to allow user to continue to enter new data while cmdSearch_Click is being called
Me.txtSearchSerialNum.SelStart = Me.txtSearchSerialNum.SelLength 'Brings curser to end of text.
End Sub
Private Sub cmdSearch_Click()
Dim startStr As String
Dim strFilter As String
MsgBox (Me.txtSearchSerialNum.Text) 'for debugging, shows the value of the text box.
If Not IsNullOrEmpty(Me.txtSearchSerialNum) Then
startStr = IIf(strFilter = "", "", " AND ")
strFilter = strFilter & startStr & " [SerialNum] like ""*" & Me.txtSearchSerialNum & "*""" 'Text field, doesn't require exact match.
End If
MsgBox (strFilter) 'for debugging, shows result of search string
If DCount("*", "qryMainInventoryManagement", strFilter) = 0 Then
MsgBox "No corresponding records to your search criteria." & vbCrLf & vbCrLf
'Call cmdReset_Click 'Need to create this button
End If
Me.frmSubInventoryManagement.Form.Filter = strFilter
Me.frmSubInventoryManagement.Form.FilterOn = True
Me.Form.Filter = strFilter
Me.Form.FilterOn = True
End Sub