How can you allow a second column in a list box, to be active?

J

JJ

Is there a way to have the second column in a list box on a form be active,
so that if you start typing a word, it will autimatically go to that word?
For instance, if you wanted to go to the word "office" which is in the second
column of a list box, you would start to type "off..." and it would position
you on the word, rather than using a scroll bar and finding it yourself.
 
K

Ken Snell [MVP]

Not directly. You'd need to put a textbox on the form and use the textbox's
Change event to mimic this. Code would run on that event that would do a
search of the listbox's second column's values to find the first one whose
starting characters match what is in the textbox, and then move the listbox
to that row.
 
J

JJ

Ken,

I appreciate your quick response. I have created some code that should be
able to do what I want. The problem I am having is that it is always one step
behind. For example, I start to type "mi" in the search box (text61), the
list box (list0) then shows all of the words starting with the letter "m". I
then continue typing and add a "c" so the search box now reads "mic", the
list box then shows all the results that start with "mi". As you can see it
is always one step back. I have attached the code I have created. Do you have
any answers?

Private Sub Text61_change()
Dim x
Debug.Print x
x = Me.Text61.Value
'Me.List0.Requery

Me.List0.RowSource = "SELECT [Job Listings Table].Company, [Job Listings
Table].[Ref #], [Job Listings Table].[Job Title], [Job Listings Table].[Job
Type], [Job Listings Table].Company FROM [Job Listings Table] WHERE ((([Job
Listings Table].Company) Like '" & x & "*')) ORDER BY [Job Listings
Table].Company, [Job Listings Table].[Job Type], [Job Listings Table].[Job
Title];"
Me.List0.Requery
Me.Repaint
Me.List0.SetFocus
Me.Text61.SetFocus
Me.Text61.SelStart = Me.Text61.SelLength
End Sub
 
J

Jamie S. Schaller

JJ,
The value of your control is not accepted as the true value until after
the control has lost focus. You have code at the end your procedure that
moves the focus from the text control to the list. This accepts the current
value of the control, which is why the control appears to be one step
behind. It is always accepting the previous value and not the current value.
There are a few work arounds that I know of, there may be a better way to
explain it, but here goes. You can either set x to the .Text property of
your control (which only works if that control has the focus) or move the
focus from Text61 control before assigning the value. Try it out and see
what happens.

JJ said:
Ken,

I appreciate your quick response. I have created some code that should be
able to do what I want. The problem I am having is that it is always one step
behind. For example, I start to type "mi" in the search box (text61), the
list box (list0) then shows all of the words starting with the letter "m". I
then continue typing and add a "c" so the search box now reads "mic", the
list box then shows all the results that start with "mi". As you can see it
is always one step back. I have attached the code I have created. Do you have
any answers?

Private Sub Text61_change()
Dim x
Debug.Print x
x = Me.Text61.Value
'Me.List0.Requery

Me.List0.RowSource = "SELECT [Job Listings Table].Company, [Job Listings
Table].[Ref #], [Job Listings Table].[Job Title], [Job Listings Table].[Job
Type], [Job Listings Table].Company FROM [Job Listings Table] WHERE ((([Job
Listings Table].Company) Like '" & x & "*')) ORDER BY [Job Listings
Table].Company, [Job Listings Table].[Job Type], [Job Listings Table].[Job
Title];"
Me.List0.Requery
Me.Repaint
Me.List0.SetFocus
Me.Text61.SetFocus
Me.Text61.SelStart = Me.Text61.SelLength
End Sub


Ken Snell said:
Not directly. You'd need to put a textbox on the form and use the textbox's
Change event to mimic this. Code would run on that event that would do a
search of the listbox's second column's values to find the first one whose
starting characters match what is in the textbox, and then move the listbox
to that row.

--

Ken Snell
<MS ACCESS MVP>

yourself.
 
Top