View Full Version : Solved: Select All and Unselect all
Djblois
12-27-2006, 03:27 PM
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
Option Explicit
Public WithEvents chb As MSForms.CheckBox
Public Sub SelectAll()
chb.Value = True
End Sub
Public Sub UnselectAll()
chb.Value = False
End Sub
Code Behind the Form: ShowColumns
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
Code behind the Select all label:
Private Sub SelectColumns2_Click()
Dim ctl As clsFormEvents
For Each ctl In col_Selection
ctl.SelectAll
Next ctl
End Sub
Code behind the unSelect all label:
Private Sub unselectcolumns2_Click()
Dim ctl As clsFormEvents
For Each ctl In col_Selection
ctl.UnselectAll
Next ctl
End Sub
I am not getting an error or anything, it just doesn't work when I click on either of the labels.
Bob Phillips
12-27-2006, 03:48 PM
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
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
Djblois
12-28-2006, 06:59 AM
When I don't use the form name I get a type mismatch error when I try to load the form
Djblois
12-28-2006, 07:04 AM
Xld,
Thank you,
Your code works
Djblois
12-28-2006, 07:13 AM
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.
Djblois
12-28-2006, 07:20 AM
Xld,
Thank you for all the help, I got it to work!!!!!!
Bob Phillips
12-28-2006, 07:24 AM
Can you share what you did for anyone who might look at this thread?
Djblois
12-28-2006, 07:39 AM
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.
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
Bob Phillips
12-28-2006, 08:21 AM
Good. Thanks
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.