Solved MultiSelect ListBox Select All Issue (1 Viewer)

LGDGlen

Member
Local time
Today, 11:09
Joined
Jun 29, 2021
Messages
229
Hi All

I have a multi select list box populate with a list of supplier that initially was quite a short list. This meant selecting all the supplier was easy. Now we have been using the system for a while the list box contents has grown and i decided to add a button to the form to select all the suppliers with the VBA code:

Code:
Private Sub btnSelectAll_Click()
    Dim intCounter As Integer
    For intCounter = 0 To Me.lstSuppliers.ListCount
        Me.lstSuppliers.selected(intCounter) = True
    Next intCounter
End Sub

This highlights ALL the suppliers in the listbox as IF they had been clicked on. The issue is that none of them technically have been clicked on. So later when i check to see if any of the list has been selected it says none have:

Code:
If Me.lstSuppliers.ListIndex <> -1 Then

This check fails. If i deselect 1 supplier and reselect them and do the check then everything goes ok.

My question is how do i actually select ALL the suppliers and not (as it would appear) highlight them as if they are selected. Do i need to loop through the
Code:
me.lstSuppliers.ItemsSelected
and check if ANY are set to true and then i know at least 1 has been selected?

just seems a bit weird to me that you say that an item is selected in the list but then the listindex doesn't show that they are until you deselect and select it

Might be that I have a fundamental misunderstanding about ListIndex and should be using something else but just a bit confused.

thanks in advance

Glen
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:09
Joined
May 7, 2009
Messages
19,231
you are almost there, use me.lstSuppliers.ItemsSelected.Count
if it is 0, non is selected.
 

LGDGlen

Member
Local time
Today, 11:09
Joined
Jun 29, 2021
Messages
229
oh @arnelgp thank you that was truly doing my head in, but that makes perfect sense
 

Users who are viewing this thread

Top Bottom