remove items from Combobox

  • Thread starter Thread starter Newbie
  • Start date Start date
N

Newbie

Hi,

I have a combobox (value list) that is populated using the additem method
when it gets the focus (contents are dependant on selection in another
combobox)

At the moment the list just gets bigger so I need to be able to remove all
entries from this combobox before it gets populated again.

I have the following code - but it is not quite right

If Me.cbxSubcon.ListCount > 0 Then
llist = Me.cbxSubcon.ListCount - 1

For lindex = 0 To llist
Me.cbxSubcon.RemoveItem (lindex)
Next
End If

I am not even sure if I should be using For....Next - how do I move between
items in a combobox?
 
Newbie said:
Hi,

I have a combobox (value list) that is populated using the additem
method when it gets the focus (contents are dependant on selection
in another combobox)

At the moment the list just gets bigger so I need to be able to
remove all entries from this combobox before it gets populated again.

I have the following code - but it is not quite right

If Me.cbxSubcon.ListCount > 0 Then
llist = Me.cbxSubcon.ListCount - 1

For lindex = 0 To llist
Me.cbxSubcon.RemoveItem (lindex)
Next
End If

I am not even sure if I should be using For....Next - how do I move
between items in a combobox?

the problem with doing it that way is that the count changes... for
example, assume you have 2 items to start so your loop runs "for lIndex=0 to
1"; on the first pass it removes item 0 leaving only 1 item left which is
now at position 0 but your second pass through the loop tries to remove item
1 and fails with an error. You can do it in a loop but you need to account
for that shifting which can be done a couple of ways:
for lIndex=0 to cbxSubcon.ListCount-1
cbxSubcon.RemoveItem 0 ' always remove the first entry
next
or, more commonly:
for lIndex=cbxSubcon.ListCount-1 to 0 step -1 ' work backwards
cbxSubcon.RemoveItem lIndex
next

of course, for removing all entries you can just:
cbxSubCon.Clear

BTW, your line:
Me.cbxSubcon.RemoveItem (lindex)
is technically incorrect since VB does not use () around arguments unless
you use CALL or using the called routine as a function. The line should be:
Me.cbxSubcon.RemoveItem lindex
-or-
Call Me.cbxSubcon.RemoveItem(lindex)

With one argument it doesn't make a big difference, with multiple arguments
you will get syntax errors.
 
Thanks - works a treat and now I know theory behind it hopefully I wont have
any more problems

Al
 
Back
Top