Listbox Delete multiple items

  • Thread starter Thread starter Luis
  • Start date Start date
L

Luis

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
 
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
 
Back
Top