Consulting

Results 1 to 9 of 9

Thread: Solved: Select All and Unselect all

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Select All and Unselect all

    I am using code I got out of a book to select all checkboxes or uncheck all checkboxes depending on what the user clicks. Unfortunatley, I can't get it to work:

    Class Module: clsFormEvents

    [vba]Option Explicit
    Public WithEvents chb As MSForms.CheckBox
    Public Sub SelectAll()
    chb.Value = True
    End Sub
    Public Sub UnselectAll()
    chb.Value = False
    End Sub[/vba]

    Code Behind the Form: ShowColumns

    [vba]Private Sub ShowColumns_Initialize()
    Dim ctl As MSForms.CheckBox
    Dim chb_ctl As clsFormEvents
    'Go through the checkboxes and add them to the frame
    For Each ctl In f_Columns2.Controls
    Set chb_ctl = New clsFormEvents
    Set chb_ctl.chb = ctl
    col_Selection.Add chb_ctl
    Next ctl
    End Sub[/vba]

    Code behind the Select all label:

    [vba]Private Sub SelectColumns2_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.SelectAll
    Next ctl
    End Sub
    [/vba]

    Code behind the unSelect all label:

    [vba]Private Sub unselectcolumns2_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.UnselectAll
    Next ctl
    End Sub[/vba]

    I am not getting an error or anything, it just doesn't work when I click on either of the labels.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    There is a load wrong wioth this code as presented.

    - col_Selection isn't defined, I guess it is a collection of clsFormEvents

    - what is f_Columns2.Controls?

    - you don't specify the form name in the form initialize event, you use the generic Userform_Initialize

    This userform code works

    [vba]

    Option Explicit

    Dim col_Selection As Collection

    Private Sub SelectColumns2_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.SelectAll
    Next ctl
    End Sub

    Private Sub unselectcolumns2_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.UnselectAll
    Next ctl
    End Sub

    Private Sub UserForm_Initialize()
    Dim ctl As Control
    Dim chb_ctl As clsFormEvents
    'Go through the checkboxes and add them to the frame
    Set col_Selection = New Collection
    For Each ctl In Me.Controls
    If TypeName(ctl) = "CheckBox" Then
    Set chb_ctl = New clsFormEvents
    Set chb_ctl.chb = ctl
    col_Selection.Add chb_ctl
    End If
    Next ctl
    End Sub

    [/vba]

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    When I don't use the form name I get a type mismatch error when I try to load the form

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Xld,

    Thank you,
    Your code works

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I understand how it works basically but how do I get it to work with 2 different collections of checkboxes on one form. Of course they will have seperate buttons and they are both inside seperate frames.

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Xld,

    Thank you for all the help, I got it to work!!!!!!

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you share what you did for anyone who might look at this thread?

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Create 2 seperate frames with checkboxes. col_Selection would be the collection for one of the frames and col_Selection2 would be the code for the second frame. The Select all and Unselect all would be put behind the controls you are using.

    [VBA]Option Explicit
    Dim col_Selection As New Collection
    Dim col_Selection2 As New Collection

    Private Sub CommandButton1_Click()
    Organize.Hide
    End Sub

    Private Sub SelectColumns_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.selectall
    Next ctl
    End Sub
    Private Sub selectInvoices_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection2
    ctl.selectall
    Next ctl
    End Sub
    Private Sub unSelectColumns_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection
    ctl.unselectall
    Next ctl
    End Sub
    Private Sub unselectinvoices_Click()
    Dim ctl As clsFormEvents
    For Each ctl In col_Selection2
    ctl.unselectall
    Next ctl
    End Sub
    Private Sub UserForm_Initialize()
    Dim ctl As Control
    Dim chb_ctl As clsFormEvents
    'Go through the checkboxes and add them to the frame
    Set col_Selection = New Collection
    For Each ctl In Columns.Controls
    If TypeName(ctl) = "CheckBox" Then
    Set chb_ctl = New clsFormEvents
    Set chb_ctl.chb = ctl
    col_Selection.Add chb_ctl
    End If
    Next ctl

    Set col_Selection2 = New Collection
    For Each ctl In Invoices.Controls
    If TypeName(ctl) = "CheckBox" Then
    Set chb_ctl = New clsFormEvents
    Set chb_ctl.chb = ctl
    col_Selection2.Add chb_ctl
    End If
    Next ctl

    End Sub[/VBA]

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Good. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •