How to reset a multi-select listBox via code?

  • Thread starter Thread starter Alp Bekisoglu
  • Start date Start date
A

Alp Bekisoglu

Hi Experts,

Did a search but couldn't find anything so I'm here once again. I couldn't
figure out how I could reset the selected items from a multi-select (simple)
listBox via a button click.

Any help is much appreciated.

Alp
 
With the function below, you can clear any list box (multiselect or not)
with:
Call ClearList([NameOfYourListBoxHere])

Function ClearList(lst As ListBox)
Dim varItem As Variant

If lst.MultiSelect = 0 Then
lst = Null
Else
For Each varItem In lst.ItemsSelected
lst.Selected(varItem) = False
Next
End If
End Function
 
Hi Allen,

Thanks a lot! once again. :-) and G'Nite mate. (I hope I got it right)

Alp

Allen Browne said:
With the function below, you can clear any list box (multiselect or not)
with:
Call ClearList([NameOfYourListBoxHere])

Function ClearList(lst As ListBox)
Dim varItem As Variant

If lst.MultiSelect = 0 Then
lst = Null
Else
For Each varItem In lst.ItemsSelected
lst.Selected(varItem) = False
Next
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Alp Bekisoglu said:
Did a search but couldn't find anything so I'm here once again. I
couldn't figure out how I could reset the selected items from a
multi-select (simple) listBox via a button click.
 
One way with MultiSelect = Simple or MultiSelect = Extended is to reset the
row source:

Me.MyListBox.RowSource = Me.MyListBox.RowSource

Another is to loop through all of the selected items, and set them back to
unselected:

Dim varItem As Variant

For Each varIem in Me.MyListBox.ItemsSelected
Me.MyListBox.Selected(varItem) = False
Next varItem

If it's a very large list box, I believe the second approach will be more
efficient.
 
Thanks Doug, both ways noted.

Alp

Douglas J. Steele said:
One way with MultiSelect = Simple or MultiSelect = Extended is to reset
the row source:

Me.MyListBox.RowSource = Me.MyListBox.RowSource

Another is to loop through all of the selected items, and set them back to
unselected:

Dim varItem As Variant

For Each varIem in Me.MyListBox.ItemsSelected
Me.MyListBox.Selected(varItem) = False
Next varItem

If it's a very large list box, I believe the second approach will be more
efficient.
 

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

Back
Top