Combobox ListIndex

  • Thread starter Thread starter Bryan Hughes
  • Start date Start date
B

Bryan Hughes

Hello,

How do you move to the next row in a combo box or listbox using a cmd
button?

-TFTH
-Bryan
 
Below is code I wrote to use up-arrow and down-arrow keys
to walk through entries in a combobox. You can adapt it
easily to what you need (it's called from the keydown
event in a combobox, and gets passed the keycode (which
you don't need) and a reference to the combobox:

Public Sub KeyDowninCB(KeyCode As Integer, theCB As
ComboBox)
Dim theItem As Long

Select Case KeyCode
Case vbKeyUp
theItem = theCB.ListIndex
If theItem > 0 Then theCB.ListIndex = theItem - 1
KeyCode = 0
Case vbKeyDown
theItem = theCB.ListIndex
If theItem < theCB.ListCount - 1 Then
theCB.ListIndex = theItem + 1
KeyCode = 0
Case Else
End Select
End Sub

Hope this helps!
 
If the cmd button is for forward movement and
the listbox is not multi-select, then you could try,

Behind the command button's click event,

if lstTest.ListIndex < lstTest.ListCount -1 then
lstTest.Selected(lstTest.listindex + 1)
else
lstTest.Selected(lstTest.listindex - 1)
end if

GL
 
If you'd like to use the Arrow Keys to navigate through a ListBox even
if the ListBox does not have the focus then read on.

Go to Form properties and set the Key Preview to Yes.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyDown
Me.List2 = Me.List2.ItemData(Me.List2.ListIndex + 1)
KeyCode = 0


Case vbKeyUp
Me.List2 = Me.List2.ItemData(Me.List2.ListIndex - 1)
KeyCode = 0

Case Else
End Select

End Sub

Tthe above code is for a ListBox named List2. Change the name to reflect
your ListBox. Also this is set to work with a ListBox WITHOUT Column
Headers turned on. You'll have to adjust it if you use Column Headers.
Finally the Arrow Keys are sent to oblivion with the Line KeyCode =0.

Your mileage may vary. :-)



--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
TFTH,

I have another question.

How do you set the current selected Item in a combo box as the default
value, so in add new rec event the Item Selected will remain.

I have tried all of the following and nothing has worked.

Const cQuote = """"

Me.cboCM.DefaultValue = cQuote & Me.cboCM.Value & cQuote

With Me.cboCM
.SetFocus
.DefaultValue = .Value
End With

-Bryan
 
Back
Top