Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear Selected Items in List Box Programmatically

Status
Not open for further replies.

pcdaveh

Technical User
Sep 26, 2000
213
0
0
US
I have a list box and I want to be able to clear the selected items programmatically. Has anyone ever done this before with out setting he rowsource of the listbox to "" the resetting the rowsource? Is there another way to do this?
 
I've found that the ActiveX control "Microsoft Forms 2.0 ListBox" it much easier to perform what you want. It's pretty easy to add and remove items from this type of list box. For help on using this ActiveX control, check out fm20.hlp on your hard drive.
 
Hi!

Depends on what you mean. If you mean to deselect them you can use:

Dim i As Integer

For i = 0 to Me!YourListBox.ListCount-1
Me!YourListBox(i).Selected = False
Next i

If you mean to remove them from the list, then it depends on the rowsource of the list box. For a value list you can use the following:

Dim varRow As Variant
Dim lngPos As Long
Dim lstBox As Control

Set lstBox = Me!YourListBox
For Each varRow in lstBox.ItemsSelected
lngPos = InStr(lstBox.RowSource, lstBox.Columns(0, varRow)
lstBox.RowSource = Left(lstBox.RowSource, lngPos - 2) &
Mid(lstBox.RowSource, lngPos + Len(lstBox.Columns(0, varRow))
Next VarRow

If you RowSource is a query or sql then you need some way of identifying what belongs in the list box and change the record on the table accordingly or replace the sql if that seems necessary.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top