Solved

removing all items from a combo box

Posted on 2008-10-07
4
741 Views
Last Modified: 2013-11-28
Hello Experts.  
I am dynamically filling a combo box based on a selection in another combo box.  On the first selection, everything works fine.  However, if the user changes his mind, I want to remove the items from the combo box and then re-add the new items based on the new selection.  To clear the combo box,  I am trying to loop through the items in the combo box and remove them using removeitem as follows:
for i = 0 to cbo.listcount-1
   cbo.removeitem(i)
next i
Note - There were 4 items in the combo box.  It loops 2 time and then I get an error "Unable to remove item . "2" not found in list.  

0
Comment
Question by:kshumway
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 22665104
try

for i = cbo.listcount-1 to 0 step-1
   cbo.removeitem(i)
next i
0
 
LVL 44

Expert Comment

by:GRayL
ID: 22665114
In my references the syntax for this method is:

Me!cbo.RemoveItem(i)

I would have written it:

for i = 0 to Me!cbo.listcount-1
   Me!cbo.removeitem(i)
next i
0
 
LVL 6

Expert Comment

by:Jammer59
ID: 22665430

I would have to agree with capricorn1.  Here is why:

If you have 4 items in the combobox they would be cbo.item(0), cbo.item(1), cbo.item(2), cbo.item(3).  Using capricorn1's logic the first time through the loop, we would remove cbo.item(3).  The combobox would now have 3 elements.  The 2nd time through the loop would remove cbo.item(2).  The combobox would now have 2 elements.  The 3rd time through the loop would remove cbo.item(1).  The combobox would now have 1 element.  The 4th time through the loop would remove cbo.item(0).  The combobox is now empty.

No offense to GRayL because anyone who is trying to help another out is ok in my book but for purely educational purposes, I think this is what will happen.  You have the same 4 items in the combobox - cbo.item(0), cbo.item(1), cbo.item(2), cbo.item(3).  The first time through the loop would remove cbo.item(0).  We now have 3 elements in the combobox.  The old cbo.item(1) is now cbo.item(0), cbo.item(2) is now cbo.item(1) and cbo.item(3) is now cbo.item(2).  By removing the first item in the combobox, we have reduced the number of elements in the combobox.  The 2nd time through the loop would remove cbo.item(1).  The cbo.item(0) element would be unchanged.  The cbo.item(2) element would become cbo.item(1).  We now have only two elements in the combobox.  The next time through the loop would attempt to remove cbo.item(2) and error would occur because at this time, the combobox only contains two elements which are cbo.item(0) and cbo.item(1).

We all know there are always several solutions to a given problem.  I will offer another which is very similar to GrayL's.  The only difference is I am always removing the first element in the combobox.

for i = 0 to Me!cbo.listcount - 1
     Me.cbo.removeitem(0)
next i
 
 
0
 

Author Comment

by:kshumway
ID: 22667796
Thank you so much.  Looping backward worked perfectly.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question