Solved

removing all items from a combo box

Posted on 2008-10-07
4
735 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 119

Accepted Solution

by:
Rey Obrero 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now