Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# removing all items from a combo box

Posted on 2008-10-07
Medium Priority
752 Views
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
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

LVL 120

Accepted Solution

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

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

LVL 44

Expert Comment

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

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

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

## Featured Post

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll

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

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