Reputation: 1359
I have a collection of multi-select listboxes on a form, filtering the records on a subform. In my implementation I have a Null record at the top of each list to represent "select all". It is working as desired.
I have a "Clear Filters" button beside the collection of listboxes to deselect any selected options in all the listboxes. Also working, except the approach leaves me at the BOTTOM of each list.
My preference is to move back to the TOP of each listbox (to the Null row), without actually selecting that row. Adding the 3 lines after the de-select loop gets me where I need to be, but feels clumsy.
Is there a better way to arrive at this same result?
Private Sub btnClear_Click()
Dim i As Integer
'Step thru the listbox options and de-select
For i = 0 To Me!lstState.ListCount
Me!lstState.Selected(i) = False
Next i
Me!lstState.SetFocus
Me!lstState.ListIndex = 0
Me!lstState.Value = ""
'...5 more list boxes not shown here
Forms!MainForm!Accounts_subform.Form.FilterOn = False
End Sub
Upvotes: 0
Views: 1192
Reputation: 32642
You're iterating forwards, so it's only logical you end up at the bottom.
Iterate backwards to end up at the top (and make sure the first item is selected to make sure you end up at the very top item):
Dim i As Long 'Selected expects a long, avoid casting for each property call
With Me!lstState
.Selected(0) = True
For i = .ListCount To 0 Step -1
.Selected(i) = False
Next i
End With
Upvotes: 1