Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete multiple items from listbox

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
US
I am trying to remove the selected items in a listbox that allows multiple selection. The code below loops the correct number of time (i.e. if I have 4 items selected, it loops 4 times), however, it prints out the first item in the list, not the items that are selected. When I actually try to remove the items, it only removes the first item in the list, not the first selected items.

Going batty over this one. Any help is appreciated.


Private Sub cmdRemove_Click()

Dim frm As Form, ctl As Control
Dim varItm As Variant

Set frm = Me
Set ctl = Me.lstProductsAssigned

For Each varItm In ctl.ItemsSelected
Debug.Print ctl.ItemData(vatItm)
Next varItm

End Sub

The only thing worse than being alone, is wishing you were.
 
OK. That does select the right one, but only the one first selected gets removed, and the others are then NOT selected anymore.

The only thing worse than being alone, is wishing you were.
 
For Each varItm In ctl.ItemsSelected
Debug.Print ctl.ItemsSelected(vatItm)
[red]Next[/red]

-DNG
 
Boy! How did I miss that. Anyways, the end result is still the same: only the first selected item is removed.

Code as it stands now:

Private Sub cmdRemove_Click()

Dim frm As Form, ctl As Control
Dim varItm As Variant

Set frm = Me
Set ctl = Me.lstProductsAssigned

For Each varItm In ctl.ItemsSelected
Me.lstProductsAssigned.RemoveItem ctl.ItemsSelected(vatItm)
Next

End Sub

The only thing worse than being alone, is wishing you were.
 
huh ha...you have varItm in the For loop and you are using vaItm

For Each [red]varItm[/red] In ctl.ItemsSelected
Me.lstProductsAssigned.RemoveItem ctl.ItemsSelected([blue]vatItm[/blue])
Next

-DNG
 
Another mistake fixed, but still the same results. I noticed that once the first item is removed, all other selected items are no longer selected. Mmmm.....

The only thing worse than being alone, is wishing you were.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top