How do you clear a listbox? (1 Viewer)

cosmarchy

Registered User.
Local time
Today, 09:34
Joined
Jan 19, 2010
Messages
116
Hi,

I've a listbox on a form which is populated with the RecordSet of a DAO query.

I want to clear the contents of the listbox but have been unsuccessful with the following.

Code:
listbox.clear
'Method or data member not found'.

Less:
listbox.RowSource = ""
or
listbox.RowSource = vbNullString
Does nothing, all the records are still in the listbox.

Code:
For i = 0 To listbox.ListCount - 1
  listbox.RemoveItem 0
Next
'The RowSourceType property must be set to Value List to use this method'

Does anyone have any other ideas?

Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:34
Joined
Oct 29, 2018
Messages
21,474
What exactly do you mean by "clearing" the listbox? Are you saying you want a blank listbox or just unselect all the items?
 

cosmarchy

Registered User.
Local time
Today, 09:34
Joined
Jan 19, 2010
Messages
116
What exactly do you mean by "clearing" the listbox? Are you saying you want a blank listbox or just unselect all the items?
I'd like to clear the listbox.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Feb 19, 2002
Messages
43,296
The RowSource is required but you can clear the selected Item.

Me.lstSomeName = Null

The RowSource is the value list. If the Listbox is bound, the instruction above will clear the selected value.

If you are working with a multi-value field, go to jail. Do not pass GO. The multi-value fields are nothing but trouble. Use a properly normalized table with a child table so you can select multiple items. Display it as a subform.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:34
Joined
May 7, 2009
Messages
19,245
just make the listbox "unbound"

Me.listbox.RowSource = ""
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:34
Joined
Sep 21, 2011
Messages
14,311
PMFJI,
What does listbox.Clear do ?

At first I thought the O/P had just made the name up :), but it does exist.
However I cannot find it mentioned at https://learn.microsoft.com/en-us/office/vba/api/access.listbox

I would say however that this worked for me?
Code:
Private Sub List5_DblClick(Cancel As Integer)
Me.List5.RowSource = ""
End Sub
 

cheekybuddha

AWF VIP
Local time
Today, 17:34
Joined
Jul 21, 2014
Messages
2,280
try it first and forget the op, maybe it is Not what he meant.
My Windows is not working at the moment so couldn't test.

I just wondered whether when you you do Set Me.listbox.Recordset = rs, then perhaps the RowSource is still vbNullstring and you have to set its Recorsdet = Nothing to clear it?
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:34
Joined
Sep 21, 2011
Messages
14,311
It comes up if you start to type it in Access though? :)
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:34
Joined
Sep 21, 2011
Messages
14,311
Yes, I only have 2007, but I entered it, intellisense made it propercase. My understanding is if it was not recognised, it would remain lowercase as I entered it?

In hospital car park atm, so cannot make any further tests.
 

jdraw

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Jan 23, 2006
Messages
15,379
If you add a button to your form(via Google):

Code:
Private Sub cmdClearList_Click()
Dim i As Integer
For i = lstFileLocation.ListCount - 1 To 0 Step -1
    lstFileLocation.RemoveItem (i)
Next i
End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 17:34
Joined
Sep 21, 2011
Messages
14,311
OK, just got home and tried again.
Whilst Clear does not appear in the dropdown, it changes from lowercase to propercase?

That is what confused me.:(

You will have to take my word for it, that I did not change the second pic manually. :)
 

Attachments

  • Clear_LC.PNG
    Clear_LC.PNG
    3.9 KB · Views: 62
  • Clear_PC.PNG
    Clear_PC.PNG
    3.9 KB · Views: 63

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Feb 19, 2002
Messages
43,296
I must be missing something. What is the reason for wanting to remove ALL values from the RowSource? There is no point to a combo or list box if it has no selection options.

You can always replace the RowSource with a query that returns no rows. Is that what you want?
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:34
Joined
May 21, 2018
Messages
8,533
Any chance this is a multiselect listbox, and by "clear" you me unselect all selections?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:34
Joined
May 21, 2018
Messages
8,533
@Gasman. If "clear" is a method of some other object I believe intellisense will change the capitalization even if not a property or method of the current combobox object.
 

Users who are viewing this thread

Top Bottom