Unselect item in list box?

G

Guest

I have several multi-select list boxes on a form. Some of them have more
items than I have room to display on the form, but the scroll bar allows the
user to see all the list items.

I also have a button for the user to click to clear all the selections:
Sub cmdClear_Click()
Me.lstElement = ""
Me.lstState = ""
End Sub

This code works great unless the user scrolls the list box so that the item
selected is no longer visible. It is the strangest thing. If I can visually
"see" the item selected and click cmdClear, the item is no longer selected
(which is what I want). If I scroll down, leaving the item selected, and
click cmdClear, the items is still selected--which I don't want. This same
scenario hold true when more than one item is selected.

I need help with how to clear what was previously selected in the list box!

Thank you,
Judy
 
G

Guest

Judy,

Try the following code for clearing all selected items from a mulit-select
list box:

'start of code
Dim varItem

For Each varItem In NameOfList.ItemsSelected
Me.NameOfList.Selected(varItem) = False
Next varItem
 
G

Guest

Judy,

Sorry, I should have also said, you need to replce the "NameOfControl" with
the actual name of your control, like:

Dim varItem

For Each varItem In lstElement.ItemsSelected
Me.lstElement.Selected(varItem) = False
Next varItem

Sorry for the double response. I just hit the Post button too quickly.
 
G

Guest

Thank you very much for responding. This is exactly what I needed--you saved
the day!

Thanks again,
Judy
 

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