Delete all items in a comboBox

jamest85

Registered User.
Local time
Today, 14:51
Joined
Jan 20, 2008
Messages
52
Hi:
I added some data into a comboBox:
Do Until rst.EOF
Me.cmboProduct.AddItem rst!ProductName
rst.MoveNext
Loop

How can I delete them by using VBA code? (I need refresh the list items).

Thanks

JT
 
The opposite of AddItem is RemoveItem. You will need to reference the index number to be able to indicate which item you want to delete. Also how are you proposing to delete them? Is it your intention to delete them from the combobox only or are you wanting to delete them from the underlying table as well?

Regards
David
 
Also, you title says "Delete all items in a comboBox." Is this really your intent? Is the combobox dynamically populated entirely by using AddItems?

ComboBox.RowSource = ""

will delete all items from the combobox for that seesion of the form. IF the combobox is populated in the control's Property Sheet it'll be re-populated on re-opening the form.
 
The opposite of AddItem is RemoveItem. You will need to reference the index number to be able to indicate which item you want to delete. Also how are you proposing to delete them? Is it your intention to delete them from the combobox only or are you wanting to delete them from the underlying table as well?

Regards
David

Hi: David, thanks for your help, the comboBox is unbound, the data is from sql server, so if I remove the data in comboBox won't effect to the data in table.

I am trying to find comboBox items count, so I can know the total number of items in the combobox, so I can use "For Loop" to delete them. but I just can't find any way to get the itemscount (or you said: index ?)

Thanks

JT
 
Also, you title says "Delete all items in a comboBox." Is this really your intent? Is the combobox dynamically populated entirely by using AddItems?

ComboBox.RowSource = ""

will delete all items from the combobox for that seesion of the form. IF the combobox is populated in the control's Property Sheet it'll be re-populated on re-opening the form.

Hi: thanks for your help.
comboBox.rowsource doesn't work for me, may be because I set the comboBox Row Source Type = Value List.

All I want to do is: delete the data in comboBox list, since the data is populated by using addItem method, so it won't delete any thing in the table. (the are not bound)

Here is what I am trying to do:
Dim i as Integer
For i = 0 To (MyComboBox.itemsCount - 1)
MyComboBox.RemoveItem(i)
Next i

(I just don't know how to get the value of:
MyComboBox.itemsCount )

Any other suggestion?

Thanks

JT
 
Last edited:
jamest85 said:
comboBox.rowsource doesn't work for me, may be because I set the comboBox Row Source Type = Value List

Works just fine for me with Row Source Type set to Value List! Does exactly what you say you want done! You did change ComboBox to the actual name of your combobox, didn't you? People frequently forget to do this, when using posted code.
 
If your Combo Box Row Source Type is set to Value List then:

Me.MyComboBox.RowSource = ""

as missinglinq so kindly pointed out should clear the Combo List. This should also clear the Combo list even if the Row Source Type is set to Table/Query. It will not however clear the Text Box area of the ComboBox if an item was selected. To do this you will need to add:

Me.MyComboBox = Null


As for retrieving the the total number of items within the Combo list you would take advantage of the ListCount property.

So by using the method provided by DCrake's, your code would look like:

Code:
Dim i As Integer
For i = Me.C.ListCount - 1 To 0 Step -1
   Me.C.RemoveItem (i)
Next i
Me.C = Null

Notice we remove from the back of the list to the front of the list. This is because the ListCount property changes every time an item is removed from the list.

.
 
If your Combo Box Row Source Type is set to Value List then:

Me.MyComboBox.RowSource = ""

as missinglinq so kindly pointed out should clear the Combo List. This should also clear the Combo list even if the Row Source Type is set to Table/Query. It will not however clear the Text Box area of the ComboBox if an item was selected. To do this you will need to add:

Me.MyComboBox = Null


As for retrieving the the total number of items within the Combo list you would take advantage of the ListCount property.

So by using the method provided by DCrake's, your code would look like:

Code:
Dim i As Integer
For i = Me.C.ListCount - 1 To 0 Step -1
   Me.C.RemoveItem (i)
Next i
Me.C = Null

Notice we remove from the back of the list to the front of the list. This is because the ListCount property changes every time an item is removed from the list.

.


Thanks, that works!
The ".ListCount" is what I am looking for.

Jt
 

Users who are viewing this thread

Back
Top Bottom