Reputation: 23187
How can I programmatically remove all items from a combobox in VBA?
Upvotes: 21
Views: 184357
Reputation: 49
If you want to simply remove the value in the combo box:
me.combobox = ""
If you want to remove the recordset of the combobox, the easiest way is:
me.combobox.recordset = ""
me.combobox.requery
Upvotes: 0
Reputation: 31
For Access VBA, if a ComboBox has been populated with a Row Source Type of Value List, I find the following works:
ComboBox.RowSource = ""
Upvotes: 2
Reputation: 11
I could not get clear to work. (Mac Excel) but this does.
ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems
Upvotes: 0
Reputation: 8720
You need to remove each one individually unfortunately:
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox using ListBox1.RemoveItem
Next i
Update - I don't know why my answer did not include the full solution:
For i = ListBox1.ListCount - 1 to 0 Step - 1
ListBox1.RemoveItem i
Next i
Upvotes: 30
Reputation: 1
Private Sub cmdClear_Click()
ComboBox1.Value = Null
ComboBox2.Value = Null
End Sub
Upvotes: -2
Reputation: 11
For Access VBA, which does not provide a .clear method on user form comboboxes, this solution works flawlessly for me:
If cbxCombobox.ListCount > 0 Then
For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
cbxCombobox.RemoveItem (remloop)
Next remloop
End If
Upvotes: 1
Reputation: 1
In Access 2013 I've just tested this:
While ComboBox1.ListCount > 0
ComboBox1.RemoveItem 0
Wend
Interestingly, if you set the item list in Properties, this is not lost when you exit Form View and go back to Design View.
Upvotes: 0
Reputation: 61
You can use the ControlFormat method:
ComboBox1.ControlFormat.RemoveAllItems
Upvotes: 6
Reputation: 131
The simplest way:
Combobox1.RowSource = "" 'Clear the list
Combobox1.Clear 'Clear the selected text
Upvotes: 13
Reputation: 39807
Psuedo code ahead (updated with actual code):
Do While ComboBox1.ListCount > 0
ComboBox1.RemoveItem (0)
Loop
Basically, while you have items, remove the first item from the combobox. Once all the items have been removed (count = 0), your box is blank.
Method 2: Even better
ComboBox1.Clear
Upvotes: 33