Listbox question

G

Guest

I am having trouble getting this code to do what I want. I am using the
following code to loop through an unbound list box and do a multi remove.
All it does is remove the first item of the multi-selected items, but I want
all the multi-selected items to be removed. I'd also like to have the list
box reset to have no items when the form is closed or a "clear" button is
clicked.

Private Sub Command5_Click()

Dim ctl As Control
Dim varItem As Variant

Set ctl = Forms!frmLBtest!lbStepIDs

For Each varItem In ctl.ItemsSelected
Me!lbStepIDs.RemoveItem ctl.ItemData(varItem)
Next varItem

Me!lbStepIDs.Requery

End Sub

Thanks in advance.
 
G

Guest

Hi, Karla.

Your multi-select list box will only remove the first item selected on the
list because the array holding the "Selected" Property of the list box is
reset to "False" for all elements in the array as soon as an item is added or
deleted from the list.

One way to work around this is to create an array to hold the value of
whether a list item is currently selected, then loop through the items in the
list from the end to the beginning of the list and save the "Selected" value
in your array. Once this is done, loop through the list backwards again and
check whether your array value at the same index has been "Selected." If it
has been selected, then remove the item in the list at that index. Continue
through the loop until all "previously selected" items have been removed.

Another way (but with more complicated code) is to save the index values of
the "Selected" items before removing any items, then create nested loops:
the outside loop would traverse through the list backwards, while the inside
loop would check the small array that only contains the indexes of the
"Selected" items in the list. If the current outside loop index matches the
value in the inside loop, then remove the item in the list. Continue through
the loops until all "previously selected" items have been removed.
I'd also like to have the list
box reset to have no items when the form is closed or a "clear" button is
clicked.

To clear all items in the list box, try:

Me!lbStepIDs.Rowsource = ""


HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts, so that all
may benefit by filtering on "Answered questions" and quickly finding the
right answers to similar questions. Remember that the best answers are often
given to those who have a history of rewarding the contributors who have
taken the time to answer questions correctly.
 

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