Multi List box De-select

S

Stockwell43

Hello,

I have a multi-select list box, text box and button on my form. When the
user makes multiple selection from the list box they click the display button
to show the selections in the textbox. Everything seems to work fine except,
the selections from the listbox are still selected and even when I go to
another record and come back it is still selected. Now if this doesn't affect
anything I'm ok with it but otherwise my question is this:

After the user clicks the button to display the selection in the text box,
at the same time is there a way to de-select the listbox so it appears as no
selection were made but not to affect the display in the textbox??

This is my first time using a list box let alone a multi-select listbox so I
am unsure of the operation in the long term especially if the selections stay
sleected in every record.

Thanks!!
 
D

Douglas J. Steele

You can loop through the list box's ItemsSelected collection:

Dim varItem As Variant

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

Alternatively, if the Multiselect property is set to 2 (Extended), you can
simply set the list box to Null:

Me!MyListBox = Null

If the Multiselect property is set to 1 (Simple), you can reset the list
box's RowSource:

Me!MyListBox.RowSource = Me!MyListBox.RowSource
 
M

Mr B

Stockwell43,

If you add the code below to end of the code in the OnClick event of the
button that you click to show the selections in the textbox, it will clear
the multi-select listbox

Dim lngListCnt As Long
lngListCnt = Me.lstBoxNameHere.ListCount - 1
For cntr = 0 To lngListCnt
Me.lstBoxNameHere.Selected(cntr) = False
Next cntr

You will need to change "lstBoxNameHere" to the actual name of your listbox.
 
S

Stockwell43

Hi Mr. B,

Thank you for responding so quickly!

I placed the code in the button at the end and changed the name. When It
selected from the list box and click the button, I got a Complie Error
"Variable not defined" and in this line of the code: For cntr = 0 To
lngListCnt it highlighted in blue "cntr"
 
D

Douglas J. Steele

Add an additional declaration

Dim cntr As Long

to the beginning of that code.
 
S

Stockwell43

Hi Doug,

Your first piece of code along with Mr B's work great. However, you sent a
second alternative. Could you please tell me which way is better or explain
thedifference in operation between the two?

Thank you both for your help, definitley most appreciated!!!
 
S

Stockwell43

Hi Guys,

Sorry but I discovered one issue. The code works great but when I leave the
record and then come back, the selections are higlighted again. Is that how
these list boxes work? If so and it will not make any difference going
forward if 500 + records are entered in the database it will still operate
correctly. Just want to make sure before it goes into production. Again, I'm
a newbie with these list boxes but they look very useful and I would like to
rely on them as an efficiency for certain databases.

Thanks!!
 
D

Douglas J. Steele

The best is setting the list box to Null, but that will only work if the
Multiselect property is 2.

The next best is looping through the selected items:

Dim varItem As Variant

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

Mr B's alternative is always as good: the difference is that it looks at
every entry in the list, while the code above only looks at the selected
entries. I doubt, though, that you'd notice any difference in speed.

The last alternative (resetting the RowSource property) can be slow if the
query's a complex one.
 

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