Link to home
Start Free TrialLog in
Avatar of kshumway
kshumwayFlag for United States of America

asked on

removing all items from a combo box

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.  

ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
 
 
Avatar of kshumway

ASKER

Thank you so much.  Looping backward worked perfectly.