Ask Your Question
0

Typing in list box to select an item?

asked 2017-04-27 05:08:45 +0200

EasyTrieve gravatar image

updated 2017-04-27 07:04:46 +0200

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.]

image description


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 flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2017-04-28 02:07:54 +0200

librebel gravatar image

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
edit flag offensive delete link more

Comments

@librebel, Thank you. That gives me something to think about. Combo boxes do what I want, but is there a way to easily edit a numeric FK using a related text string w/ a combo? Correct me if I'm wrong, but combos are limited to 1 field, whereas lists-boxes allow 2. So I'm trying to make a decision; do I write a macro to deal w/ combo boxes failings, or do I write macros to deal w/ list box failings. Prob. I should work on the combo boxes, as any code in list boxes is bound to get sluggish.

EasyTrieve gravatar imageEasyTrieve ( 2017-04-28 05:19:37 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-27 05:08:45 +0200

Seen: 339 times

Last updated: Apr 28 '17