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?
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?
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 ?)
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 )
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.