Advent Day 6 Ms Access Gurus

VBA > Control > Listbox > Clear List

Clear the selection of a listbox control

Screen shot

The list on the left shows 3 items are selected. Then the Clear List button is clicked. The result on the right shows the selection has been cleared.

Example


example from code behind the form, perhaps on the Click event of a command button:

Call ClearList( Me.myListBox_Controlname )

Logic

Loop through each selected item of the passed listbox control using the ItemsSelected collection, and set the Selected property of each to False. Then SetFocus to the listbox control (this is optional, of course). Skip errors, if there are any. This works for a multi-select listbox, as well as a listbox that is not.

Code

'*************** Code Start *****************************************************
' Purpose  : Clear the selection of a listbox control 
' Author   : crystal (strive4peace) 
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'--------------------------------------------------------------------------------

' ClearList

'-------------------------------------------------------------------------------- '
Public Sub ClearList( _ oCtl As Control _ ) '181206 s4p On Error Resume Next Dim vItem As Variant With oCtl For Each vItem In .ItemsSelected .Selected(vItem) = False Next vItem .SetFocus End With End Sub ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************

Share

Share with others ... here's the link to copy:
https://MsAccessGurus.com/VBA/Code/control_ClearList.htm