From: Luis on
Hello.
I'm trying to create a vba routine to delete items from a listbox.
This is a multiselect listbox and what is happening is that i select several
items and the code only removes the first item. When the first item is
deleted, all of the other selected items became unselected.
I post the code i'm using bellow.
Can anyone give me a hint on this issue?

Thanks,
Luis


Private Sub RemoveItem_btn_Click()
If Listbox_1.ListIndex = -1 Then Exit Sub

For i = Listbox_1.ListCount - 1 To 0 Step -1
If Listbox_1.Selected(i) Then
Listbox_1.RemoveItem (i)
End If
Next i
End Sub





From: Allen Browne on
Loop through the ItemsSelected in the listbox, building up a string to use
the in WHERE clause of a DELETE query statement.

The core idea is to end up with code like this:
Dim strSql As String
strSql = "DELETE FROM Animals WHERE AnimalName IN ("dog", "cat",
"fish");"
dbEngine(0)(0).Execute strSql, dbFailOnError

For an example of building the WHERE clause by looping through
ItemsSelected, see:
http://allenbrowne.com/ser-50.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Luis" <Luis(a)discussions.microsoft.com> wrote in message
news:2AB28923-1632-474F-A59E-29A04BD73391(a)microsoft.com...
> Hello.
> I'm trying to create a vba routine to delete items from a listbox.
> This is a multiselect listbox and what is happening is that i select
> several
> items and the code only removes the first item. When the first item is
> deleted, all of the other selected items became unselected.
> I post the code i'm using bellow.
> Can anyone give me a hint on this issue?
>
> Thanks,
> Luis
>
>
> Private Sub RemoveItem_btn_Click()
> If Listbox_1.ListIndex = -1 Then Exit Sub
>
> For i = Listbox_1.ListCount - 1 To 0 Step -1
> If Listbox_1.Selected(i) Then
> Listbox_1.RemoveItem (i)
> End If
> Next i
> End Sub
>
>
>
>
>