Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cycling through values in a MS Access list box

I have a list box that populates with different sets of data based on user selections.

How can I cycle through any given values that may be in the list box? Is this a For Each statement, or what?

like image 598
Justin Avatar asked May 28 '10 23:05

Justin


Video Answer


2 Answers

Here is how you iterate through the ListBox:

Dim i as Integer

For i = 0 to Me.ListBoxName.ListCount -1
   'Access each item with 
   'Me.ListBoxName.ItemData(i)
Next i
like image 50
Robben_Ford_Fan_boy Avatar answered Sep 23 '22 14:09

Robben_Ford_Fan_boy


You can do a For loop to examine each row in the listbox, and do whatever with the rows which are selected. In this example, I display the second column from selected items in the lstLocations listbox. (Column numbering starts with zero.)

Private Sub cmdShowSelections_Click()
    Dim lngRow As Long
    Dim strMsg As String

    With Me.lstLocations
        For lngRow = 0 To .ListCount - 1
            If .Selected(lngRow) Then
                strMsg = strMsg & ", " & .Column(1, lngRow)
            End If
        Next lngRow
    End With

    ' strip off leading comma and space
    If Len(strMsg) > 2 Then
        strMsg = Mid(strMsg, 3)
    End If
    MsgBox strMsg
End Sub

Note I assumed you want the selected items from the list box. If you want all items, selected or not, you could use .ItemData as @DavidRelihan suggested. However, in that case, you could get them from the listbox .RowSource instead.

like image 45
HansUp Avatar answered Sep 23 '22 14:09

HansUp