Solved

removing all items from a combo box

Posted on 2008-10-07
4
747 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

732 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