Move to next row in listbox

  • Thread starter Thread starter p
  • Start date Start date
P

p

How can i move, through vba, to the next selection in a single-
selection listbox?



* I did look through helpfiles & newsgroups for such a simple question
to no avail.
 
Here's an example. In this example lstTest is the list box and cmdUp and
cmdDown are command buttons. Remember, though, that the user can scroll
through the items in a list box using the up and down arrow keys, so I
can't, off-hand, think of many scenarios in which this code would be very
useful.

Private Sub cmdDown_Click()
If Me.lstTest.ListIndex < Me.lstTest.ListCount - 1 Then
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListIndex + 1)
Else
Me.lstTest = Me.lstTest.ItemData(0)
End If
End Sub

Private Sub cmdUp_Click()
If Me.lstTest.ListIndex > 0 Then
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListIndex - 1)
Else
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListCount - 1)
End If
End Sub
 
Here's an example. In this example lstTest is the list box and cmdUp and
cmdDown are command buttons. Remember, though, that the user can scroll
through the items in a list box using the up and down arrow keys, so I
can't, off-hand, think of many scenarios in which this code would be very
useful.

Private Sub cmdDown_Click()
If Me.lstTest.ListIndex < Me.lstTest.ListCount - 1 Then
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListIndex + 1)
Else
Me.lstTest = Me.lstTest.ItemData(0)
End If
End Sub

Private Sub cmdUp_Click()
If Me.lstTest.ListIndex > 0 Then
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListIndex - 1)
Else
Me.lstTest = Me.lstTest.ItemData(Me.lstTest.ListCount - 1)
End If
End Sub
sweet thanks..

lets just say its a reporting tool ~ prompting the user to go to the
next record in a list in order to fill a number of tasks (which
populate the list)
 

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

Back
Top