Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type-ahead for list box? 1

Status
Not open for further replies.

duck72

Programmer
Dec 9, 2002
5
US
I'm using two side-by-side list boxes to select people from a list. (Once selected from the left-hand list, their names disappear from there and appear in the right-hand list.) This is like what the Form Wizard or Report WIzard does with fields.

This is working fine, but it would be much nicer if the lists worked more like an Windows Explorer file list window, where typing the first few characters of the desired file name will move the selection to a matching file. In the list boxes, only the FIRST character typed is matched. When you type a second character, the selection moves to a name that starts with that character. So typing "Smi" selects not Smith but Ibrahim. Is there any way to make the list box work as desired?

Jim Beard
 
Yes, this can be done. I have it working quite well.

Basically, you need to trap keystrokes delivered to the list box, appending them to a string that represents the characters typed so far. Use the KeyPress event to trap the character.
- If the keycode is 0, vbKeyReturn, or vbKeyTab, just exit so Access can handle these.
- If the keycode is vbKeyEscape, clear your string. (I use this as a way to let the user start over, rather than backspacing through what they've typed so far.)
- If the keycode is vbKeyBack, delete the rightmost character from your string.
- If the keycode is anything else, translate it to a character (using Chr$()) and append it to your string. Then walk through the list box finding the matching item and set the list box's Value property to the ItemData value of the matching row.

For the last 3 conditions, make sure you set the keycode parameter to 0 before you exit the event procedure. If you don't, the listbox will reprocess the keystroke and do its own scrolling, nullifying the effect of yours.

You will probably also want to clear the search string if the user clicks in the list box, so if they use the mouse for one item and then start typing for the next one, their typing starts from scratch. Clear the search string in the AfterUpdate event.

There are some subtle bugs that may come up having to do with event recursion. If you run into one, let me know.


Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Duck72,

Have you thought about using a combo box? It has the same basic functions as the list box but also includes the ability you're looking for.

The Missinglinq

"It's got to be the going,
not the getting there that's good!"
-Harry Chapin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top