Workaround for "Select all listbox items after requery" bug (1 Viewer)

cnstarz

Registered User.
Local time
Today, 06:53
Joined
Mar 7, 2013
Messages
89
I found out first-hand that there's a weird bug in Access where if you requery a listbox and then programmatically select all of its items, it will show some items as not selected even though they really are.

For example... Run the following code:

Code:
Private Sub txt_Search_Change()
    
'// Update filter listbox
    Dim l As Long
    
    With Me.lbx_stuff
        'Requery listbox
        .Requery
        
        'Select all listbox items
        For l = 0 To .ListCount - 1
            .Selected(l) = True
        Next
    End With
    
End Sub
Yet there are still apparently unselected items:



Looping through and debug.printing the listbox's .SelectedItems confirms they're all really selected, they just don't look like it. After a few hours of playing around with it, I finally found a work-around: Unselect all items first before requerying the listbox, then select all items again. The new procedure looks like this:

Code:
Private Sub txt_Search_Change()
'// Update filter listbox to show
'   counts of current recordsource
    Dim l As Long
    Dim v As Variant
    
    With Me.lbx_stuff
        'Unselect all listbox items
        For Each v In .ItemsSelected
            .Selected(v) = False
        Next v
        
        'Requery listbox
        .Requery
        
        'Select all listbox items
        For l = 0 To .ListCount - 1
            .Selected(l) = True
        Next
    End With
    
End Sub
And, magically, everything is now selected every time the listbox is refreshed:



Hopefully this is helpful for someone in the future (and can serve as a reminder for myself). I've attached a sample database with the forms so you can see it in action.
 

Attachments

  • select all listbox.zip
    31.8 KB · Views: 63

Users who are viewing this thread

Top Bottom