Listbox--actions based on selected items

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.
 
D

Douglas J Steele

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
 
G

Guest

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
 

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

Top