Listbox--actions based on selected items

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a listbox with maybe 5 things in it. The user will select from 1 to 5
of those things, and then click a button.

When the button is clicked, I want to go through every item in the listbox,
create a corresponding record in TableA for every selected item, and create a
corresponding record in TableB for every non-selected item.

I was using a For Each loop to go through each row in
Me.listBox.ItemsSelected and then using a DAO recordset with AddNew to add
records for each selected item, but I don't know what kind of loop to use to
look at EVERY item in the list box and then check each one to see if it's
selected or not.

I'm sure it's probably a simple solution; I just can't get it.

Thanks.
 
Dim intLoop As Integer

For intLoop = 0 To Me.listBox.ListCount
If Me.listBox.Selected(intLoop) Then
' it's selected
Else
' it's not selected
End If
Next intLoop

If you've set ColumnHeads to True, start at 1, not 0. You can accomplish
this generically using:

For intLoop = IIf(Me.listBox.ColumnHeads, 1, 0) To Me.List0.ListCount
 
Dim intCount as Integer
Dim intMax as Interger

intMax = Me!MyListBox.ListCount - 1
For intCount = 0 to intMax
If Me!MyListBox.Selected(intCount) Then
'Do the Selected stuff here
Else
'Do the Not Selected stuff here
End If
Next intCount
 
Back
Top