Typing in list box to select an item?

Is there some setting for list-boxes that would allow the letters typed into a list box to simply select the first item with those initial letters.

Like for example if I type "exa" it would skip down to the word "example", if that was the first word to use those three initial letters.

I created this simple example database with Form1 which looks up US state names so you can take a look at this issue. Just run Form1 and try to look for state names. The only thing that does make sense is if you type 'a' 5 times, it gets the 5th item starting with a. But what if you have 100 names starting with a? Then this approach makes no sense.

[Edit: I've updated this Form1 to use a query which lists states first and territories 2nd to show you a more potent example of this issue.]

Another example would be when I have some records which are obsolete, I like to sort them to the end. This makes the current, non-obsolete records the primary selectors, without having to sift through the obsolete records, but if an older record is pointed to it still shows up correctly. An it makes it so with a little more trouble you can even insert an obsolete record. It would look something like this:

Item1
item2
item3
...
item99 -obsolite
item100 - obsolite
..

The point is that if you type "item1", most of the time you're NOT interested in "item100", so it should go to item1 unless you type item10, and then it goes to item 100.

Does anyone know how to make this thing work better?

edit retag close merge delete

Sort by » oldest newest most voted

Hello EasyTrieve, even though ComboBoxes are usually better suited for AutoCompletion than Listboxes, i once wrote a method for selecting a Listbox row based on a key press from the user. Perhaps this method could be helpful in your case...

Sub ListboxSelectFirst(oListbox, KeyChar as String, AfterPattern as String)
REM The normal behaviour for Listboxes when the user presses a Character Key on the Keyboard,
REM is to advance its selection to the next row that starts with that Character.
REM This Method does the same, except that it will advance its selection to the next row containing that Character,
REM appearing immediately AFTER a specified String Pattern occuring in the Listbox rows.
Dim i as Integer, curPos as Integer, row as String
Dim AllRows() as String
On Error Goto Exit_Sub
curPos = oListbox.SelectedItemPos
AllRows = oListbox.Items
For i = curPos + 1 To oListbox.ItemCount - 1
row = Mid$(AllRows(i), InStr(AllRows(i), AfterPattern) + Len(AfterPattern)) If StrComp(Left$(row, Len(KeyChar)), KeyChar, 0) = 0 Then
oListbox.selectItemPos(i, TRUE)
Exit Sub
End If
Next
For i = 0 To curPos - 1
row = Mid$(AllRows(i), InStr(AllRows(i), AfterPattern) + Len(AfterPattern)) If StrComp(Left$(row, Len(KeyChar)), KeyChar, 0) = 0 Then
oListbox.selectItemPos(i, TRUE)
Exit Sub
End If
Next
Exit_Sub:
End Sub

more