0

I am trying to filter my listbox lstOutputon the fly as the user types into my textbox txtInput however none of the key events on my textbox fire until I click my listbox and then go back to my textbox.

Here is the code I am using

Private Sub txtInput_KeyPress(KeyAscii As Integer)
Dim strSQL As String

strSQL = "SELECT Patient.PatientID, Patient.FName, Patient.LName " & _
    "FROM Patient " & _
    "WHERE (((Patient.FName)=[Forms]![New Session]![txtInput]) OR ((Patient.LName)=[Forms]![New Session]![txtInput])) " & _
    "ORDER BY Patient.[FName], Patient.[LName];"

Me.lstResults.RowSource = strSQL
Me.lstResults.Requery
End Sub

I have tried the KeyUp, KeyDown, Dirty, Change events as well but to avail. The only one that works is the AfterUpdate event but I won't the results to filter as the user types and not after pressing enter.

1 Answer 1

1

OnChange is the event you want to use, this event triggers whenever you make a change to the TEXT in the textbox. Now the default property of a textbox is VALUE. As you type the .TEXT of the textbox is changed, when you hit enter/tab the .VALUE is changed. You need to reference to the .TEXT like:

[Forms]![New Session]![txtInput].Text

This should hopefully get you started

2
  • Ok now I feel just silly. This worked perfectly. One thing though, would it better to do what I am doing via VBA or use a query that I have already created? Creating a query in design mode is easier for me since I can use expression builder and drag and drop.
    – Chris
    Commented Dec 22, 2015 at 10:28
  • @Chris If designing the query in design mode is easier for you I think there's no need to change something you're comfortable with :) (I assume you meant your "strSQL")
    – DivisionX
    Commented Dec 22, 2015 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.