Auto populate listbox with textbox

PatAccess

Registered User.
Local time
Yesterday, 20:33
Joined
May 24, 2017
Messages
284
Hello Guys,
I am trying to get a listBox to auto-populate or change as I am typing in the textbox. but I type all my selections go away. I placed the code under _Change event AND _KeyPress but nothing. Am I doing this right?
I went online to research some codes but I want to believe it is not THAT complicated?
Here is what I have
Code:
Private Sub txtSearch_Change()
    Dim sqlTraining As String
    sqlTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees WHERE [Training] LIKE "" * " & Me.txtSearch & " * "" ORDER BY [Training]"
    Me.lstTraining.RowSource = sqlTraining
    'Me.lstTraining.Refresh
    Me.lstTraining.Requery
End Sub
 
If you use the textbox's Change event, you should refer to its .Text property vs. .Value, (you are implicitly referring to .Value by not specifying any property - which is precisely why I don't recommend people do this, it obfuscates and avoids learning the actual property usages.)

I'm also not sure about all your quotation marks.

Try
Code:
sqlTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees WHERE [Training] LIKE '*" & Me.txtSearch & "*' ORDER BY [Training]"
 
If you use the textbox's Change event, you should refer to its .Text property vs. .Value, (you are implicitly referring to .Value by not specifying any property - which is precisely why I don't recommend people do this, it obfuscates and avoids learning the actual property usages.)
You don't recommend people do what?
And with the .Text Property, do you mean
Code:
sqlTraining = "SELECT DISTINCT [Training] FROM QryTrainedEmployees WHERE [Training] LIKE "" * " & Me.txtSearch.Text & " * "" ORDER BY [Training]"
Because I tried with that but it is still not working?
Thank you
 
Older versions of Access required that you use the .text property when your code was running in a control that had the focus. It is the property you want to use for example in the on Change event so you can check each character as it is entered. Once focus leaves a control, you would refer to it using the .Value property or since .Value is the default you could omit it. So,
Me.mycontrol
Me.mycontrol.Value
me.mycontrol.Column(0) (combo)

ALL refer to the same thing, assuming that the bound column of a combo is the first column in the RowSource)

Your version of Access might allow references to .Text from other events but it is almost certainly not allowed in a query. I also think that .Value is not allowed in a query. You just use the control name - Forms!myform!mycontrol

So, I think Isaac was saying that he always uses Me.mycontrol.Value. That's fine. It is explicit but I use the Me.mycontrol format.
 
Use the text box as isaac said. Use single quote inside the double quotes. Like "'*" & textSearch.text & "*'"
Double Single * Double & textSearch & Double * Single Double
like '*Abc*'
 
I do a lot of find as you type. Listboxes, comboboxes, forms. I want a lot of features so I have build a few classes. This way I can build any FAYT with a single line of code and do not have to mess with the sql string to avoid this problem.
 

Users who are viewing this thread

Back
Top Bottom