Button to select next record in unbound combo box (1 Viewer)

daze

Registered User.
Local time
Today, 08:27
Joined
Aug 5, 2009
Messages
61
Hi, all

I have unbound combo box based on query.
I'd like to have another button which select next record in that unbound combo...
(similar to next record in bound form).

Is this possible? I've tried using code to select ListIndex of that combo box but it gives me error - Invalid use of ListIndex property.

Ideas?
 

liddlem

Registered User.
Local time
Today, 07:27
Joined
May 16, 2003
Messages
339
I have NO idea if this will work, but ....
Could you have a list box (perhaps covering the combo box?) that has the toggles attached.
Then, when the user clicks up/down, the value of the hidden combo box is changes, which in turn triggers a requesry to your data?
 

missinglinq

AWF VIP
Local time
Today, 02:27
Joined
Jun 20, 2003
Messages
6,423
Here's code for two buttons, one moves down to the next record, the other up to the previous record.

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 button again, it wraps back to the first selection; if you're on the first selection and hit the Up button it wraps around to the last selection.

You'll need to replace ComboName, of course, with the actual name of your Combobox.

Next selection
Code:
Private Sub MoveDown_Click()
ComboName.SetFocus
If ComboName.ListIndex <> ComboName.ListCount - 1 Then
        ComboName.ListIndex = ComboName.ListIndex + 1
      Else
        ComboName.ListIndex = 0
      End If
End Sub
Previous selection
Code:
Private Sub MoveUp_Click()
ComboName.SetFocus
If ComboName.ListIndex <> 0 Then
        ComboName.ListIndex = ComboName.ListIndex - 1
      Else
        ComboName.ListIndex = ComboName.ListCount - 1
      End If
End Sub

Don't have your code, but the Invalid use of ListIndex property error may be popping because you didn't set focus to the Combobox before doing your manipulation.

Linq ;0)>
 

daze

Registered User.
Local time
Today, 08:27
Joined
Aug 5, 2009
Messages
61
missinglinq,

This code actually works well, but

my problem is that unbound combo moves to next (or whatever selection there is) record of current form as number of macros - there is event AfterUpdate...

I'll have to test it and see what macro is doing problems.


Thanks, code works exactly what I needed.
 

missinglinq

AWF VIP
Local time
Today, 02:27
Joined
Jun 20, 2003
Messages
6,423
When Controls are populated thru code, its associated events, such as AfterUpdate, etc., do not fire. You have to explicitly Call the event.

Code:
Private Sub MoveDown_Click()
ComboName.SetFocus
If ComboName.ListIndex <> ComboName.ListCount - 1 Then
        ComboName.ListIndex = ComboName.ListIndex + 1
      Else
        ComboName.ListIndex = 0
[B]Call ComboName_AfterUpdate[/B]
      End If
End Sub
You'd have to do the same for the MoveUp button, as well.

Linq ;0)>
 

davidrbetts

New member
Local time
Today, 07:27
Joined
Jul 15, 2011
Messages
3
Hi - I am using the code supplied by missinglinq to move from one record to the next. I am using the after update property of the combo box to apply a filter to the subform that the box is in. It all works perfectlly unless there are no records in the subform that meet the filter criteria. In this case i get a run time error 7777 you've used the list index incorrectly. Any ideas on how to fix this would be appreciated. Thanks. Dave.
 

Aviqq

New member
Local time
Today, 01:27
Joined
Apr 27, 2023
Messages
4
Here's code for two buttons, one moves down to the next record, the other up to the previous record.

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 button again, it wraps back to the first selection; if you're on the first selection and hit the Up button it wraps around to the last selection.

You'll need to replace ComboName, of course, with the actual name of your Combobox.

Next selection
Code:
Private Sub MoveDown_Click()
ComboName.SetFocus
If ComboName.ListIndex <> ComboName.ListCount - 1 Then
        ComboName.ListIndex = ComboName.ListIndex + 1
      Else
        ComboName.ListIndex = 0
      End If
End Sub
Previous selection
Code:
Private Sub MoveUp_Click()
ComboName.SetFocus
If ComboName.ListIndex <> 0 Then
        ComboName.ListIndex = ComboName.ListIndex - 1
      Else
        ComboName.ListIndex = ComboName.ListCount - 1
      End If
End Sub

Don't have your code, but the Invalid use of ListIndex property error may be popping because you didn't set focus to the Combobox before doing your manipulation.

Linq ;0)>
Hi - I am using the code supplied by missinglinq to move from one record to the next. I am using the after update property of the combo box to apply a filter to the subform that the box is in. It all works perfectlly unless there are no records in the subform that meet the filter criteria. In this case i get a run time error 7777 you've used the list index incorrectly. Any ideas on how to fix this would be appreciated. Thanks. Dave.
I have the same issue. If the combo box has a selection made, then I get the runtime error 7777 Listindex property error. Once I clear the selection from the combo box, then I don't get the error. Missinglinq, do you know how to fix this?
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:27
Joined
Sep 21, 2011
Messages
14,232
Almost a 12 year old thread? :(

Please start your own. I am not sure if you can refer to this thread after just joining, but you can try and post a link to this thread, if you feel it is needed.
 

Users who are viewing this thread

Top Bottom