By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,595 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,595 IT Pros & Developers. It's quick & easy.

Using Arrow Keys in Combo Box

P: 206

Is there a way to program the down arrow key on the keyboard to move to the next item in a combo box list?

The reason I want to do this is that my "Ordered By" combo box on my form is populated by a Customers table. Some of the companies in our customers list have multiple offices, each of which counts as a separate customer. For Example "RGS Tile, LLC" has offices in Arlington, Lake Ridge, Fredericksburg, etc. So in the combo box they are listed as "RGS Title, LLC (Arlington)", "RGS Title, LLC (Lake Ridge), etc.

The autocomplete feature fills in the first customer in the list alphabetically, so for example, when I type "RG" that is enough to cause "RGS Title, LLC (Arlington)" to pop up. However, the RGS office in Lake Ridge is our most frequent customer. So I find myself needing to frequently highlight the "(Arlington)" portion of the text to change it to "(Lake Ridge)"

I would like to be able to type "RG", let it pop up, then hit the down key a couple times until "RGS Title, LLC (Lake Ridge)" shows up. I don't want to have to use the drop-down, and I don't want to have to do the mouse highlight/type over thing every time. It's too tedious.

Aug 11 '08 #1
Share this Question
Share on Google+
5 Replies

Expert 2.5K+
P: 3,532
I wrote this for someone who had a similar requirement. Just replace all instances of YourComboBox with the actual name of your cbo.

Expand|Select|Wrap|Line Numbers
  1. Private Sub YourComboBox_KeyDown(KeyCode As Integer, Shift As Integer)
  3.   Select Case KeyCode
  4.     Case vbKeyDown
  5.       If YourComboBox.ListIndex <> YourComboBox.ListCount - 1 Then
  6.         YourComboBox.ListIndex = YourComboBox.ListIndex + 1
  7.       Else
  8.         YourComboBox.ListIndex = 0
  9.       End If
  10.    Case vbKeyUp
  11.       If YourComboBox.ListIndex <> 0 Then
  12.         YourComboBox.ListIndex = YourComboBox.ListIndex - 1
  13.       Else
  14.         YourComboBox.ListIndex = YourComboBox.ListCount - 1
  15.       End If
  16. End Select
  18. End Sub
In order to keep focus from leaving the combobox when you get to the beginning or the end of the selections, I have it "wrapping." If you get to the last selection and hit the Down Arrow again, it wraps back to the first selection ; if you're on the first selection and hit the Up Arrow it wraps around to the last selection.

From your post, it would appear that you reside in the historical Commonwealth of Virgina! If so, which part?

Linq ;0)>
Aug 11 '08 #2

P: 206
This sounds perfect. Thanks!

Woodbridge, Virginia
Aug 11 '08 #3

P: 1
(i'm only a beginner, and not trained as a programmer - i'm an industrial engineer, but here was my solution to the same question, only found it a different way). it allows you to use the combo box similar to the way google's home page does - you can start typing till it finds what u want

In the properties for the combo box, under the GotFocus, add an event procedure, using the code below. just replace the "cboYourName" with your combo box's name

Private Sub cboYourName_GotFocus()


End Sub
Nov 6 '08 #4

Expert 2.5K+
P: 3,532
If you read the original post carefully, you'll see that the OP stated

I don't want to have to use the drop-down, and I don't want to have to do the mouse highlight/type over thing every time. It's too tedious.
Alternate solutions are always welcome here, but please make sure that they fulfill the stated needs of the original poster.

Welcome to Bytes!

Linq ;0)>

Nov 7 '08 #5

P: 1
listindex is read only, I believe.

This is the right answer (function).

Expand|Select|Wrap|Line Numbers
  2. Public Sub ArrowKeys(ByRef KeyCode As Integer, combo As ComboBox, ByRef Flag As Boolean)
  3. ' flag is used to be able to toggle normal action in combo_change()
  4. Select Case KeyCode
  5.     Case vbKeyDown
  6.         KeyCode = 0 ' make sure the action wont be duplicated
  7.         If combo.ListIndex <> combo.ListCount - 1 Then
  8.           combo.Value = combo.ItemData(combo.ListIndex + 1)
  9.           Flag = True
  10.           combo.Dropdown
  11.         Else
  12.           combo.Value = combo.ItemData(0)
  13.           Flag = True
  14.           combo.Dropdown
  15.         End If
  17.    Case vbKeyUp
  18.         KeyCode = 0 ' make sure the action wont be duplicated
  19.         If combo.ListIndex = -1 Then ' In case nothingg is selected yet (-1 index)
  20.           combo.Value = combo.ItemData(combo.ListCount - 1)
  21.           combo.Dropdown
  22.         ElseIf combo.ListIndex <> 0 Then
  23.           combo.Value = combo.ItemData(combo.ListIndex - 1)
  24.           Flag = True
  25.           combo.Dropdown
  26.         Else
  27.           combo.Value = combo.ItemData(combo.ListCount - 1)
  28.           Flag = True
  29.           combo.Dropdown
  30.         End If
  31. End Select
  32. End Sub
Aug 23 '20 #6

Post your reply

Sign in to post your reply or Sign up for a free account.