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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to go to next item in combo box

Status
Not open for further replies.

Spaniard

MIS
Jul 23, 2002
29
0
0
US
I have a combo box that gets values from a table. A user selects a value and clicks btnSave. How do I get the combo box to automatically select the next value? I would like it to be in the Click event of the Save button, but am not sure of the code.

Thanks,
SKW
 
Hi!

You can use this code:

Dim intRow As Integer

intRow = Me!YourListBox.ListIndex

Me!YourListBox.Selected(intRow) = False

If intRow = Me!YourListBox.ListCount - 1 Then
Me!YourListBox.Selected(intRow - 1) = True
Else
Me!YourListBox.Selected(intRow + 1) = True
End If

This will select the next item unless the item selected is the last item in which case it will select the item before it.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Thanks for the quick reply, but alas, the code doesn't work (Access says the object doesn't support the property RTE '438'). At first I thought it was the fact that it was a combo box, but the problem persisted even after changing it to a list box.
 
Forget about the error message, I resolved that, but the code stays on the same value and doesn't bump to the next value.
 
Hi!

Which property didn't it like? The Selected property is available only with a list box and only if it is not multiselect. For a combo box, just set the value of the box to the data in the next row like this:

Dim intRow As Integer

intRow = Me!YourComboBox.ListIndex

If intRow = Me!YourComboBox.ListCount - 1 Then
Me!YourComboBox.Value = Me!YourComboBox.Column(0, intRow - 1)
Else
Me!YourComboBox.Value = Me!YourComboBox.Column(0, intRow + 1)
End If

hth
Jeff Bridgham
bridgham@purdue.edu
 
OK, I think I'm on track...I moved it from the button click and placed the code in the form after_update event. BUT...it says I may be at the end of a recordset (RTE 2105). What could be causing this?

Thanks for your patience with a dunderhead such as I...

SWK
 
Hi!

The after update event is probably the wrong event because after the update the selection is gone. Try creating the intRow as a form level variable (in general declarations) then in the button click you set the variable the listindex and in the form load procedure set the variable to -1, finally, in the form current event use this code:

If intRow <> -1 Then
If intRow = Me!YourComboBox.ListCount - 1 Then
Me!YourComboBox.Value = Me!YourComboBox.Column(0, intRow - 1)
Else
Me!YourComboBox.Value = Me!YourComboBox.Column(0, intRow + 1)
End If
End IF

hth
Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top