Set False works, Set True doesn't??? (1 Viewer)

George-Bowyer

Registered User.
Local time
Today, 07:20
Joined
Dec 21, 2012
Messages
177
Hello,

I've been using this code to uncheck all items in a list box for ages (can't remember where I got it from - somewhere here, probably) - and it works fine.


Code:
    Dim varItm As Variant

    With lstWhatever

        For Each varItm In .ItemsSelected
            .Selected(varItm) = False
        Next varItm

    End With

I just tried using it to check all of the boxes, which I assumed would just require changing "False" to "True", but bizarrely, the button does nothing.


Can anyone please explain this?

Thanks,

George
 
Last edited:

June7

AWF VIP
Local time
Yesterday, 22:20
Joined
Mar 9, 2014
Messages
5,463
The code is only cycling through items that have been selected. They are already 'True'. If no items in listbox have been selected then there is nothing for the code to act on.

But what do you mean by 'check all of the boxes'? Exactly what 'boxes' are checked? A listbox doesn't have any place to enter a 'check'.
 

Minty

AWF VIP
Local time
Today, 07:20
Joined
Jul 26, 2013
Messages
10,366
There is an function below that you can call to do this on any list, courtesy or Allen Browne;

Code:
Public Function SelectAll(lst As ListBox) As Boolean
    On Error GoTo Err_Handler
    'Purpose:   Select all items in the multi-select list box.
    'Return:    True if successful
    'Author:    Allen Browne. http://allenbrowne.com  June, 2006.
    Dim lngRow As Long

    If lst.MultiSelect Then
        For lngRow = 0 To lst.ListCount - 1
            lst.Selected(lngRow) = True
        Next
        SelectAll = True
    End If

Exit_Handler:
    Exit Function

Err_Handler:
    Call LogError(Err.Number, Err.Description, "SelectAll()")
    Resume Exit_Handler
End Function
 

George-Bowyer

Registered User.
Local time
Today, 07:20
Joined
Dec 21, 2012
Messages
177
There is an function below that you can call to do this on any list, courtesy or Allen Browne;

Code:
Public Function SelectAll(lst As ListBox) As Boolean
    On Error GoTo Err_Handler
    'Purpose:   Select all items in the multi-select list box.
    'Return:    True if successful
    'Author:    Allen Browne. http://allenbrowne.com  June, 2006.
    Dim lngRow As Long

    If lst.MultiSelect Then
        For lngRow = 0 To lst.ListCount - 1
            lst.Selected(lngRow) = True
        Next
        SelectAll = True
    End If

Exit_Handler:
    Exit Function

Err_Handler:
[COLOR="Red"]    Call LogError(Err.Number, Err.Description, "SelectAll()")
[/COLOR]Resume Exit_Handler
End Function

Thanks. The above line in red throws a "sub not defined" error.

I presume that this is because Allen Browne assumes that one is using his
error handling code as well?
 

June7

AWF VIP
Local time
Yesterday, 22:20
Joined
Mar 9, 2014
Messages
5,463
Yes, so change it to:

MsgBox Err.Number & " : " & Err.Description & " : " & "SelectAll()"
 

Users who are viewing this thread

Top Bottom