Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove items from combo box

Status
Not open for further replies.

oaquao

Programmer
May 16, 2001
12
US
Hello,

I have 2 combo boxes cmbMuscleGroup cmbMusclePart.
When I choose an item from cmbMuscleGroup ex.chest the corresponding items appear in the cmbMusclePart ex. upper,middle,lower
If a mistake was made and I wanted to change cmbMuscleGroup,
I want to remove the items from cmbMusclePart.
Clear method not available in VBA.
Tried numerous methods, code generates a error unable to remove item 2 not in list, am I missing something, or is there another method.

Any help would be appreciated.
oaquao

If cmbMusclePart.Value <> &quot; &quot; Then
cmbMusclePart.Value = &quot; &quot;
Dim i As Integer
Dim listCount As Integer
listCount = cmbMusclePart.listCount
'For i = 0 To cmbMusclePart.listCount
'For i = 0 To listCount
For i = 0 To listCount - 1
cmbMusclePart.RemoveItem i
Next i
 
Have you tried RemoveAllItems

The problem you are experiencing is likely due to the fact that each time you remove an item the remaining items get reindexed. Therefore there will come a point when you are trying to remove an item that no longer exists.

Example if you start off with 4 items in the list
Max index = 3,you remove item(0),remaining items reindexed.
Max Index = 2,You remove item(1),remaining items reindexed.
Max Index = 1,you remove item(2),Error: item(2) does not exist

You could try reversing the loop
Example:
For i = listCount - 1 To 0 Step -1
cmbMusclePart.RemoveItem i
Next i

 
Tried reversing the loop same result.
RemoveAllItems generates a compiler error.
At least I realize the remaining items are getting reindexed.

Thank you,
oaquao

 
FYI this works.

For i = 0 To listCount - 1
cmbMusclePart.RemoveItem 0
Next i

oaquao
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top