How To Clear Off List Box With VBA Code

D

doyle60

How do you clear a list box with VBA code? That is, if the user has
blackened in items, how do I clear them off so nothing is chosen?

Thanks,

Matt
 
M

Marshall Barton

How do you clear a list box with VBA code? That is, if the user has
blackened in items, how do I clear them off so nothing is chosen?


For a single select list box, set its value to Null.

I seem to remember that multi select list boxes are
troublesome. Try setting it Row source to itself:
Me.listbox.RowSource = Me.listbox.RowSource
 
D

Duane Hookom

Add this code to a standard module and call it from code in your form like:

ClearListBox Me.lboDepts

Sub ClearListBox(pctlListBox As ListBox)
'============================================================
' Purpose: clear all selection from a list box control
' Programmer: Duane Hookom
' Called From: Multiple
' Date: 2/21/2003
' Parameters: list box object
'============================================================
On Error GoTo ClearListBox_Err
Dim strErrMsg As String 'For Error Handling

Dim varItem As Variant
For Each varItem In pctlListBox.ItemsSelected
pctlListBox.Selected(varItem) = False
Next


ClearListBox_Exit:
On Error Resume Next
Exit Sub

ClearListBox_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) &
vbCrLf
strErrMsg = strErrMsg & "Error Description: " & Err.Description
MsgBox strErrMsg, vbInformation, "ClearListBox"
Resume ClearListBox_Exit
End Select
End Sub
 
M

Marshall Barton

Duane, I seem to remember that there were situations(?)
where that didn't work, probably back in A97.
 
D

Duane Hookom

I seem to remember something earlier on also. This function is a staple in
most of my Access applications along with code to build a "where clause" of
selected items etc.

--
Duane Hookom
MS Access MVP


Marshall Barton said:
Duane, I seem to remember that there were situations(?)
where that didn't work, probably back in A97.
--
Marsh
MVP [MS Access]


Duane said:
Add this code to a standard module and call it from code in your form
like:

ClearListBox Me.lboDepts

Sub ClearListBox(pctlListBox As ListBox)
'============================================================
' Purpose: clear all selection from a list box control
' Programmer: Duane Hookom
' Called From: Multiple
' Date: 2/21/2003
' Parameters: list box object
'============================================================
On Error GoTo ClearListBox_Err
Dim strErrMsg As String 'For Error Handling

Dim varItem As Variant
For Each varItem In pctlListBox.ItemsSelected
pctlListBox.Selected(varItem) = False
Next


ClearListBox_Exit:
On Error Resume Next
Exit Sub

ClearListBox_Err:
Select Case Err
Case Else
strErrMsg = strErrMsg & "Error #: " & Format$(Err.Number) &
vbCrLf
strErrMsg = strErrMsg & "Error Description: " &
Err.Description
MsgBox strErrMsg, vbInformation, "ClearListBox"
Resume ClearListBox_Exit
End Select
End Sub
 
M

Marshall Barton

Duane said:
I seem to remember something earlier on also. This function is a staple in
most of my Access applications along with code to build a "where clause" of
selected items etc.


If you haven't seen a problem, would you say that the
problem was fixed?

If so, why weren't we told about it? <gdr>
And, would care to hazard a guess as to when it was fixed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top