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





0
Utf
2/18/2010 10:21:02 AM
access.forms 6864 articles. 2 followers. Follow

1 Replies
3110 Views

Similar Articles

[PageSpeed] 41

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@discussions.microsoft.com> wrote in message 
news:2AB28923-1632-474F-A59E-29A04BD73391@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
>
>
>
>
> 
0
Allen
2/19/2010 6:49:46 AM
Reply:

Similar Artilces:

outlook deletes original message on yahoo account, how do i stop t
how do i stop outlook from deleting the original message on yahoo mail account Version of outlook? Go to tools, email accounts, open the account go to more settings and leave mail on the server (last tab) -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com Outlook Tips by email: dailytips-subscribe-request@lists.outlooktips.net EMO - a weekly newsletter about Outlook and Exchange: EMO-NEWSLETTER-SUBSCRIBE-REQUEST@PEACH.EASE.LSOFT.COM You can access this newsgroup by visiting http://www.microsoft...

Multiple Conditions Formula Help Needed
I currently have a formula that I have used to calculate commisio rates. We had a 2 rate plan. If sales were below a certail threshol then apply this rate and if above apply this rate. I used an IF/THE formula. We have recently switched to a 4 rate plan, and I am unsur how to create a formula to reflect this. Please advise.. thanks dav -- Message posted from http://www.ExcelForum.com One approach would be a table to use with VLOOKUP. In the formula, where you now have a rate, use VLOOKUP. On Wed, 21 Jul 2004 16:53:11 -0500, chiwavdg <<chiwavdg.19rkim@excelforum-nospam.com>...

Publisher 2007 multiple duplex prints problem
Hi, When printing a duplex program, A4, it works fine when printing 1. However, when printing multiple copies it messes it up. It wil put page "1 & 4" on the one side, and again on the other side, then the next one comes out with page 2 & 3 printed on both sides. This is with automatice duplexing. Works fine in publisher 2003, the problem started in 2007. The only way around it is to click print 100 times if we need 100 sets. Is this a know bug and is there a solution? Tks mlungu wrote: > When printing a duplex program, A4, it works fine when printing 1. However, ...

data in single column split to multiple col's based on header
Hi All, I have data in three diff columns, e.g: column A column B column C 1 absman a 1 folklore b 2 internet c 2 absman d 3 folklore e 3 internet f I want these to be segregated under different heads based on the unique ID in column A with header (col B) and pull the data in Col C Result shld be something like this Headet absman folklore internet 1 a b 2 d c 3 Could ypu please help or I need to run vlookup by splitting the data, I have 17 million rows of data (split in 2 files) to be pulled under 16 heads. Your help in this regard is highly appreciated. -...

Can I restore emails deleted from POP3 after I had setup outlook?
Can I restore emails deleted from POP3 after I had setup outlook? After setup I realized there is an option to retain new mail on the server, but all past emails have been deleted. ...

Auto Deleting Rows Based on Cell Data
Hi and thanks up front for any help you might can give me. I've go several rows of data that I need to filter out and I'd rather not hav to review each line individually to do this. Here's what I'd like t be able to do... Example: I have columns A-M and in column D, there is either a Y or an N. I' like to do something that would automatically delete every row that ha an N in column D. Is this possible -- Message posted from http://www.ExcelForum.com Hi see: http://www.xldynamic.com/source/xld.Deleting.html -- Regards Frank Kabel Frankfurt, Germany > Hi and tha...

delete characters values
Hello All! I have a column with both characters values and numeric values. I would like to delete from that column only character values. Does someone know how to do it? Thanks in advance regards Emanuela This will require a custom vba function to strip out the non-numeric characters. Paste the two functions below into a VBA module and call one of them in your query. They work slightly different. Field: fStripToNumbersOnly(YourFieldName) Public Function fStripToNumbersOnly(ByVal varText As Variant) As String 'Takes input and returns only the numbers in the input. Strips out '...

Multiple users
How do I set up multiple users to receive e-mail in outlook.I go into tool, account and add,then I fill out all the information, and an error occurs when mail is trying to be received. ...

Open Other User�s Folder doesn�t show Sent Items
I am looking for a registry hack on the Open Other User�s Folde function in Outlook. My user needs to access the SENT ITEMS folder o her supervisor, but the default folders that are available are onl Calendar, Contacts, Inbox, Journal, Notes, Tasks. This is in th drop-down when you go to File>Open>Other User's Folder. Does anyone know how to alter this behavior and expand the choices o available folders -- bankboysbPosted from - http://www.officehelp.i You will need to use a different approach. To access a shared folder = that isn't one of the folders listed on the File | Op...

Total count of multiple fields?... Is that possible?
Here are the fields I need to manipulate Field 1: Account (Text field with validation rule) Field 2: Closed (Date field) Field 3: Termination (Yes/No) I want to be able to get a total count of records not closed or terminated. The condition should be to count everything except for the ones that are "Closed" and also marked for "Termination". My concern is that if the termination field is "checked" ... it may or may not have an entry in the closed field which may count the records twice. The termination field is used because the case may have bee...

Deleted group still receive mail
I have created a global distribution group, add three users to it. Three users receiving the mails that are sent to this distribution group. After 2-3 months later (today), I added one more user to this group, but this user cant get the mails from the outside (namely from hotmail). The mails sent to this dstribution group inside the exchange organization are received from the users I updated Receipent Update Service, but no way the user cant get the mails posted to distribution group. Then it is amazing that; I deleted the distribution group,updated Receipent Update Service,restart Exchange...

Deleting Worldcom
G'd day, I'd like to stop reminding myself that I ever bought some Worldcom every single time I open my portfolio, but I'd like to keep the record. Is "selling" the stock for a price of zero the only way to eliminate the line from my portfolio? thanx, Ed In microsoft.public.money, <anonymous@discussions.microsoft.com> wrote: > >I'd like to stop reminding myself that I ever bought some >Worldcom every single time I open my portfolio, but I'd >like to keep the record. Is "selling" the stock for a >price of zero the only w...

Creating a new document from a multiple paged Excel document
Can I save only one page of an existing Excel multiple page document? If so, how? Thanks to anyone that can take the time to answer/instruct. Right-click the sheet tab that you want, and hit Move or Copy. Choose "Create a copy" and then, from the dropdown, choose "new book". **** Hope it helps! **** ~Dreamboat Excel VBA Certification Coming Soon! www.VBAExpress.com/training/ ******************************** "Lisa" <Lisa@discussions.microsoft.com> wrote in message news:CBBDECC6-2432-41D4-99F2-C1EF10B1EC1F@microsoft.com... > Can I save only one page of...

Multiple but different copies per page
I am laying out an A6 booklet. To save paper, I want to print 4 different pages to an A4 page, which I'll print out to proof. How can I acheive this in Publisher 2003? (I can only find an option to print the same A6 page, 4 times, on an A4) Open a new instance of Publisher, divide the page in quarters, copy/paste from the booklet. -- Mary Sauer http://msauer.mvps.org/ "Allan TFF" <AllanTFF@discussions.microsoft.com> wrote in message news:4C9F3FA2-5DBC-4AEC-86AE-7359895372A4@microsoft.com... >I am laying out an A6 booklet. To save paper, I want to print 4 differ...

scrollbar listbox
i'm trying to get rid of the horizontal scrollbar in listbox is it even possible to do that? tia jocke --- Message posted from http://www.ExcelForum.com/ ...

automatically generate item look up code
is there anyway RMS can automatically generate items look up code according to items department. for example if we add an item in a department called "cigar" the item lookupcode should have a prefix of category "cigar" which can be "01" and then a serialized lookupcode. for example if the last item entered in the cigar category has code "01150" the next item we add in this department should automatically get the lookupcode as "01151". can anybody please give me any idea on how can i go about doing this. any advice or comment will be mu...

Delete numbers
I need to import a price list from Excel to SAP. The problem is that need to import a price with only two digits after the period lik 63.45. The problem is I was sent a price list with many digits lik 63.4578932 or 12.3268246987. I would like to permanently delete the digits forward and just keep the two digits after the period as i 63.45 or 12.32. I know about the increase and decrease decimal. But need to permanently delete the digits. Can anyone help? Thank you Roge -- Rogerm ----------------------------------------------------------------------- Rogermb's Profile: http://www.exc...

Can I delete C:/WSUS?
Hi, I'm running SBS2008 Standard, with all service packs and updates. I have WSUS installed (of course) and moved the WSUS installation to my large D: drive where it happily consumes 15 GB of data. I notices that I have a C:\WSUS folder that takes up 3.7 GB, which isn't the end of the world but I'd like to save that space if possible. It contains C:\WSUS\UpdateServicesDbFile\SUSDB.mdf. Is is safe to delete this since "everything" has moved to the D drive? Thanks, - Mark That is your WSUS database file so you don't want to delete it. "Ps...

how do i add multiple addresses to one label sheet
i am using publisher on windows xp, and using address template L7159, i have mail merged my addresses but, i want to have a whole page of addresses not just the one address through out the sheet. how do i change it so i can have 24 different addresses? Mail merge -- JoAnn Paules MVP Microsoft [Publisher] "ketchupsuicide" <ketchupsuicide@discussions.microsoft.com> wrote in message news:1D977963-F3A3-4518-B379-327768BDE026@microsoft.com... >i am using publisher on windows xp, and using address template L7159, i >have > mail merged my addresses but, i want to...

How to print labels tags for all items "on sale"
Is there any way to print tags, labels for all items that are currently on sale ? I have a report that shows me itms that are on sale but i do not know of a way to print labels for all items without selecting them one by one. Any help would be helpful. Robert Robert, Sadly, there is nothing like this built into RMS. They have a Print All Labels With This Filter option, but Sale Start Date, Sale End Date, and Sale Price are all missing from this list. That was one of the major reasons that we developed our RITE Label program, which does this and a whole lot more. If ...

How can I add multiple source data data labels?
To be more exact, I require my original data labels (in an Area Chart in Excel) to remain visible, ie £250,444; in addition I require to show on the same drop line, either above or below this original figure, the Loan To Value amount, ie 48%. I am sure this is possible, however I am unable to discover the correct process to enable this eventuality. Hi, First you need to create a concatenated formula in the spreadsheet, then you need to use a custom add-in to use those cells a data labels. Assume you 250,444 is in column B and the 48% is in column C then in column D enter thr...

On deletion of rows
Hi Say I open a spreadsheet up that has 2000 rows, 3 columns I delete rows 100-1900 (not clear them, they really are deleted). So 200 rows left I would expect the keystroke "Ctrl-End" to take me to cell 200C. It does not. Instead I end up on cell 2000C. Somehow, Excel has retained the knowledge of the old position and not updated itself to the new end position.How do I get Excel to update its knowledge of the bottom-rightmost cell? Using Excel 2000, 9.0.4402, SR-1 Thanks Stephen Howe Delete all rows/columns below and to the right of your data - then save the file, close and...

duplicate sent items
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Email Client: imap Dear sir or Madam, <br> Help I am using an AOL IMAP account setup in entourage all works fine but.. every time I send a message to anyone, a copy of the message is put in the sent items folder this is normal and the way it should be however after sending the message, I get a new mail notice sound only to find a second copy of the sent mail in my sent folder. So I am left with 2 copies of the same message in my sent folder. How can I tell entourage to only keep one copy not two. Help...

Unknown error trying to delete items in Outbox
I have an eternal-september.org account and there are two messages stuck in my Outbox that I can't delete because it says "an unknown error has occurred". Has anyone seen this or have a hint as to why it would say this? Should I go directly to the folder and delete the items? Mike "MHL" <booyakasha at mac dot com> wrote in message news:OJFBSVgbKHA.5608@TK2MSFTNGP05.phx.gbl... > I have an eternal-september.org account and there are two messages stuck > in my Outbox that I can't delete because it says "an unknown error has >...

recover deleted items #4
Is there a way to hide or remove the recover delted items option. I would like to do this with by user or computer if possible ...