If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Listbox Delete multiple items
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 |
#2
|
|||
|
|||
Listbox Delete multiple items
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" wrote in message ... 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 |
Thread Tools | |
Display Modes | |
|
|