A Microsoft Office (Excel, Word) forum. OfficeFrustration

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.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Listbox Delete multiple items



 
 
Thread Tools Display Modes
  #1  
Old February 18th, 2010, 10:21 AM posted to microsoft.public.access.forms
luis
external usenet poster
 
Posts: 87
Default 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  
Old February 19th, 2010, 06:49 AM posted to microsoft.public.access.forms
Allen Browne
external usenet poster
 
Posts: 11,706
Default 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 01:47 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.