Do not use the KeyPress event, you will not be able to easily restore the actual input data from the event handler itself (when the event is triggered, the key just pressed is not yet added to the text field text, meaning that you are always short with the last key press).
Instead, use the KeyDown and use the text < .Text instead of .Value .
.Value set only after the focus is removed from the window.
Thus, your code can simply be rewritten as (make sure your KeyDown event is set in text field events on the form):
Private Sub TxtVendorSearch_KeyDown(KeyCode As Integer, Shift As Integer) ' If there is no filter criteria, disable filtering ' If Len(Trim(TxtVendorSearch.Text)) = 0 Then subOrderDS1.Form.FilterOn = False Exit Sub End If ' Do nothing if user hits RETURN or TAB ' If KeyAscii < 32 Then KeyCode = 0 Exit Sub End If Dim str1 As String str1 = "[VendorID] LIKE '*" & Trim$(TxtVendorSearch.Text) & "*'" subOrderDS1.Form.Filter = str1 subOrderDS1.Form.FilterOn = True End Sub
I used Trim() to remove any start and end white space that the user could type.
Last: you do not need to use Me. or Me! from the form code itself.
It doesn't hurt if you do this, but it makes things a little less legible without adding anything to the code.
source share