Help needed in looping through a multi-select listbox (1 Viewer)

kurto55

New member
Local time
Today, 19:09
Joined
Sep 28, 2015
Messages
1
Hi all -

I have (hopefully) a simple problem that I need help with. I have a simple form that has a multi-select list box that is populated by another table. The list box shows one value (the 3rd column), but the table has 3 columns (the first two are not shown. When the user clicks the button, I want to populate a new record with each of the three variables. The number of grantees selected shows the right number, but I seem to be having trouble moving through the selected records. Here is my code so far, but this only shows the values of the last selected item over and over. I seem to be having trouble looping though the selected items.

Private Sub Command4_Click()

If Me.List2.ItemsSelected.Count = 0 Then
MsgBox "No grantee has been selected - please try again"
Else

MsgBox "Number of grantees selected: " & Me.List2.ItemsSelected.Count

Dim x As Variant

For Each x In Me.List2.ItemsSelected

MsgBox Me.List2.Column(0)
MsgBox Me.List2.Column(1)
MsgBox Me.List2.Column(2)

Next

End If
End Sub



Any help would be much appreciated!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:09
Joined
May 7, 2009
Messages
19,231
Private Sub Command4_Click()

If Me.List2.ItemsSelected.Count = 0 Then
MsgBox "No grantee has been selected - please try again"
Else

MsgBox "Number of grantees selected: " & Me.List2.ItemsSelected.Count

Dim x As Variant
Dim strSelected as String

For Each x In Me.List2.ItemsSelected

strSelected = strSelected & Me.List2.Column(x,0) & " " & _
Me.List2.Column(x, 1) & " " & _
Me.List2.Column(x, 2) & vbCrLf

Next
MsgBox strSelected
End If

End Sub
 

Users who are viewing this thread

Top Bottom