sooprise
sooprise

Reputation: 23187

Removing All Items From A ComboBox?

How can I programmatically remove all items from a combobox in VBA?

Upvotes: 21

Views: 184357

Answers (12)

bigjoepro75
bigjoepro75

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

Lars49
Lars49

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

Andrew Watkins
Andrew Watkins

Reputation: 11

I could not get clear to work. (Mac Excel) but this does.

ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems

Upvotes: 0

Henning Lee
Henning Lee

Reputation: 584

me.Combobox1.Clear

This is the common method

Upvotes: 0

Robben_Ford_Fan_boy
Robben_Ford_Fan_boy

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

Robert
Robert

Reputation: 1

Private Sub cmdClear_Click()
    ComboBox1.Value = Null
    ComboBox2.Value = Null
End Sub

Upvotes: -2

Tim Wray
Tim Wray

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

Pat Harkin
Pat Harkin

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

David
David

Reputation: 61

You can use the ControlFormat method:

ComboBox1.ControlFormat.RemoveAllItems

Upvotes: 6

Hendry Halim
Hendry Halim

Reputation: 131

The simplest way:

Combobox1.RowSource = ""  'Clear the list
Combobox1.Clear           'Clear the selected text

Upvotes: 13

Mr_Hmp
Mr_Hmp

Reputation: 2535

Best Way:

Combobox1.items.clear();

Upvotes: 4

Tommy
Tommy

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

Related Questions