Trying to use Keyboard arrows to move through a combo box

B

BillD

I have a combo box that I would like to use the arrow keys on my keyboard to
navigate through. The combo box is to select states. When I type in the first
letter I get for example Maine. I would like to be able to use my arrow keys
to move to another state for example Minnesota.

I was give a code to type in but I now get a run time error 424 whenever I
try to enter anything in the texbox or use the arrow on the text box.
The error code reads: Run Time Error '424'; Object Required.
When I click on the debug button I get the code which is below and the line
"If KeyCode = VbKeyDown and Combobox.ListIndex <> Combobox.ListCount - 1
Then" is highlighted in yellow. I did a copy paste so the below code is
exactly as it appears in my database.

Private Sub CoState_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown And ComboBox.ListIndex <> ComboBox.ListCount - 1 Then
ComboBox.ListIndex = ComboBox.ListIndex + 1
End If
If KeyCode = vbKeyUp And ComboBox.ListIndex <> -1 Then
ComboBox.ListIndex = ComboBox.ListIndex - 1
End If
End Sub

I am still learning Acess so if you could explain what I need to do in
simple terms I would appreciate it.

Thank you for your time and help
 
B

Bob Gajewski

Bill

Couple of options here.

1) If you do not have AutoComplete set to yes, subsequent pressing of the
"M" key (in your specific example) will cycle through all of the "M" states.

2) If you press F4 while in the combobox, it will dropdown and you can use
the arrow keys to navigate up and down.

3) You can set the combobox to automatically dropdown when entering, and
then you can use the arrow keys to navigate up and down.

I prefer option #2, but try them all first and see which you like.

Regards,
Bob Gajewski
 
B

BillD

Where do I find the autocomplete. I looked in the properties and did not see
any option titled "Autocomplete". Also if I wanted to try option 3 how do I
set the combobox to automatically drop down when I enter a letter in the box.

I have tried option 2 and that works ok, but would like to try the others
also to see which works the best for me

Thanks
 
B

Bob Gajewski

Sorry, Bill - terminology error. The property is "Auto Expand", not
autocomplete. And I'm not so sure that this one works the right way.

The other is a property you set in code. Using the OnEnter and/or GotFocus
events, just put:

cboStateCode.Dropdown = True

Regards,
Bob
 
D

Douglas J. Steele

What's the name of your combo box? The name of the routine is
CoState_KeyDown, which implies that the combo box is named CoState, yet
you're using ComboBox.ListIndex inside the routine.

Try changing ComboBox.ListIndex to CoState.ListIndex or, better,
Me!CoState.ListIndex.
 
B

BillD

Changing Combobox to CoState worked I can't believe I missed that, too many
hours looking at the computer I guess.
One thing I noticed and I don't know if there is a fix for it or not, but
when I type in a letter for example "M" then Maine shows in the combobox.
However, when I arrow down the next state comes up and the cursor moves to
the next field to the right in the tab order. If I press the up arrow the
cursor moves to the previous field in the tabl order.
Is there anyway to stop that from happening. If there is could you please
explain it in some simple terms I would appreciate that.
Thanks for the eye opener and also for any other help you can provide me on
this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top