Clearing Selections in a Multi-Select List Box

A

Ashley

I have a Multi Select List Box called States on a Form named
frmStartupscreen. The user selects the different states they need and then
that information is pulled by a query. Once that query is run, there is
sometimes a need for them to re run the Query with new states selected.

Is there a way to put an extra button on the form that will clear out all
the selections in the multi-select list box?

I hope this makes sense. Please let me know if I need to explain anything
further.

Thanks
 
K

Ken Snell MVP

Use code like this:

Private Sub CommandButtonName_Click()
Dim lngLoop As Long
For lngLoop = 0 To Me.NameOfListBox.ListCount - 1
Me.NameOfListBox.Selected(lngLoop) = False
Next lngloop
End Sub
 
D

Douglas J. Steele

<picky>

Private Sub CommandButtonName_Click()
Dim varSelected As Variant

For Each varSelected In Me.NameOfListBox.ItemsSelected
Me.NameOfListBox.Selected(varSelected) = False
Next varSelected

End Sub

might be quicker, as it only worries about the ones that are selected, as
opposed to every item in the list.

</picky>
 
K

Ken Snell MVP

Douglas J. Steele said:
<picky>

Private Sub CommandButtonName_Click()
Dim varSelected As Variant

For Each varSelected In Me.NameOfListBox.ItemsSelected
Me.NameOfListBox.Selected(varSelected) = False
Next varSelected

End Sub

might be quicker, as it only worries about the ones that are selected, as
opposed to every item in the list.

</picky>

Ok, I'll save this for future replies! < g >
 
D

Douglas J. Steele

Actually, there are simpler approaches too.

If the the Multiselect property is Simple (1) or Extended (2), you can clear
all selected entries by resetting the RowSource property:

Me.NameOfListBox.RowSource = Me.NameOfListBox.RowSource

If the Multiselect property is Extended (2), you can clear all selected
entries by setting the list box to Null (the same as you'd do if the
Multiselect property was None (0):

Me.NameOfListBox = Null
 

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