clear a listbox

  • Thread starter Thread starter mcnewsxp
  • Start date Start date
mcnewsxp said:
how does one clear a listbox in access 2003?
lbxMyBox.clear don't do it.

You can try simply setting the listbox's value to "Null" if "MultiSelect" is set
to "None":

Me.lbxMyBox.Value = Null

If "MultiSelect" is set to "Simple" or "Extended, you can cycle through each
selected row and set its "Selected" property to false. You can use the following
function to do this:

'************FUNCTION START********
Public Function fncClearListBox(ctl As ListBox)
' Comments : Clears selected rows from multiselect listbox
' Parameters: ctl - A reference to the listbox to clear
' Returns : <none>
' Created : 11/30/04 14:02 Bruce M. Thompson
' Modified :
' Usage : fncClearListBox Forms("frmTest").lstListBoxName
' ... or, if called from within the form containing the listbox ...
' fncClearListBox Me.lstListBoxName
' --------------------------------------------------

On Error GoTo fncClearListBox_ERR

'Declare variables
Dim varItem As Variant

'Unselect the selected rows
For Each varItem In ctl.ItemsSelected
ctl.Selected(varItem) = False
Next

fncClearListBox_EXIT:
Exit Function

fncClearListBox_ERR:
MsgBox "Error " & Err.Number & " occurred in fncClearListBox: " &
Err.Description
Resume 'fncClearListBox_EXIT

End Function
'************FUNCTION END**********

Copy the function to a standard module (a global module, not a module behind a
form or report) and call it using the same syntax used in the examples provided
in the function's comments section.
 
this works


If lbxReportDate.ListCount > 0 Then
'Unselect the selected rows
For x = 0 To lbxReportDate.ListCount - 1
lbxReportDate.RemoveItem Index:=lbxReportDate.ItemData(x)
Next x
End If
 
this works
If lbxReportDate.ListCount > 0 Then
'Unselect the selected rows
For x = 0 To lbxReportDate.ListCount - 1
lbxReportDate.RemoveItem Index:=lbxReportDate.ItemData(x)
Next x
End If

Aha! I didn't realize that you were removing rows of data from the listbox,
*not* deselecting them (it obviously contains a list, as opposed to looking up
values via a table or query). And does this actually remove all of the entries?
I haven't encountered the need to do this (this is only available in the last
one or two versions of Access), but I wonder if the listbox's index is reset
after removing the each item. If this were the case, it would only remove every
other entry.

Incidentally, if the listbox contained a SELECT clause as the rowsource, you
would need only to set the rowsource to "" (or vbNullString).
 
I take it this listbox has its RowSourceType set to "Value List".

It would be much simpler to set the RowSource to a null string:
lbxReportDate.RowSource = ""

It would also be more effective, because I would expect the code below to
remove only every *second* item.
 
Back
Top