Select multiple checkboxes with a label (1 Viewer)

GrahamUK33

Registered User.
Local time
Today, 03:33
Joined
May 19, 2011
Messages
58
I have a form with a number of checkboxes that can be selected independent of each other.

As an additional feature, is there a simple way of adding a click event to lblTitle to select/deselect (tick/untick) all five checkboxes together?

lblTitle
chkCheckBox1
chkCheckBox2
chkCheckBox3
chkCheckBox4
chkCheckBox5
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:33
Joined
Sep 21, 2011
Messages
14,417
labels do not have events.?


You could try a double click to select/unselect all perhaps?
Double click on any, sets/unsets them?
 

jleach

Registered User.
Local time
Yesterday, 22:33
Joined
Jan 4, 2012
Messages
308
Sure - on the Click event of the label:

Code:
Private Sub lblTitle_Click()
  Me.chkCheckBox1.Value = True
  Me.chkCheckBox2.Value = True
  Me.chkCheckBox3.Value = True
  Me.chkCheckBox4.Value = True
  Me.chkCheckBox5.Value = True
End Sub
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
Or

Code:
Private Sub lblTitle_Click()

Dim I As Integer

For I = 1 to 5
  Me("chkCheckBox" & I) = True
Next

End Sub

.Value NOT required as its the default
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:33
Joined
Sep 21, 2011
Messages
14,417
Sure - on the Click event of the label:

Code:
Private Sub lblTitle_Click()
  Me.chkCheckBox1.Value = True
  Me.chkCheckBox2.Value = True
  Me.chkCheckBox3.Value = True
  Me.chkCheckBox4.Value = True
  Me.chkCheckBox5.Value = True
 End Sub


Learnt something new again today.:eek:
My apologies for leading the o/p astray, I was looking at the label for a control.
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
Learnt something new again today.:eek:
My apologies for leading the o/p astray, I was looking at the label for a control.

Labels do have a smaller number of events but I use the label click event regularly
 

GrahamUK33

Registered User.
Local time
Today, 03:33
Joined
May 19, 2011
Messages
58
Or

Code:
Private Sub lblTitle_Click()

Dim I As Integer

For I = 1 to 5
  Me("chkCheckBox" & I) = True
Next

End Sub

.Value NOT required as its the default

Thanks for having a look at this, I am getting an Run-Time error 2465 using the code below:

Code:
Private Sub lblTitle_Click()

Dim I As Integer

For I = 1 to 5
  Me("chkCheckBox1" & I) = True
  Me("chkCheckBox2" & I) = True
  Me("chkCheckBox3" & I) = True
  Me("chkCheckBox4" & I) = True
  Me("chkCheckBox5" & I) = True
Next

End Sub
 

jleach

Registered User.
Local time
Yesterday, 22:33
Joined
Jan 4, 2012
Messages
308
.Value NOT required as its the default

This is a bit of a pet peeve of mine - .Value is not the default... a reference to the control object itself is what you point to when you don't use .Value. The Value property is what gets resolved if the runtime determines that you're trying to assign a basic type to something, but it is not the default - it is a result of not being explicit and the runtime trying to inherently decide what you wanted.

The problem comes when people get used to omitting the .Value because it works in most cases without out, then try to assign Me.MyCtl to a variant and wonder why they get a type mismatch because they wound up with an object reference instead of a value type, or when they do a <> comparison against a nullable type, or any number of other situations where a .Value is required.

When it comes to programming, I find that the less ambiguity you leave in your code, the less trouble we tend to run into over the long term.

Cheers,
 

jleach

Registered User.
Local time
Yesterday, 22:33
Joined
Jan 4, 2012
Messages
308
Your runtime 2465 is because you didn't copy Colin's code exactly.

The method he's using is using an integer counter to represent the 1-5 portions of your checkbox names, then looping while incrementing the number each time. It's unnecessary (and incorrect) to put all control names within there.

Try it exactly as he posted and use the debugger to step through the code and you should see how that approach works. It's a very helpful method in many situations.
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
As Jack has already stated, the error was because you modified the code by combining both versions. As a result the code is looking for 25 checkboxes 11-15, 21-25, 31-35 etc which don’t exist

Use my code or Jack’s but not a mixture of the two


Sent from my iPhone using Tapatalk
 

GrahamUK33

Registered User.
Local time
Today, 03:33
Joined
May 19, 2011
Messages
58
I am now using the code that you posted:

Code:
Private Sub lblTitle_Click()

Dim I As Integer

For I = 1 to 5
  Me("chkCheckBox" & I) = True
Next

End Sub

I am still getting an error:
Run-time error 2465
Microsoft Access can't find the field 'chkCheckBox1' referred to in your expression.

I used chkCheckBox1, chkCheckBox2, etc as an example. If the checkboxes were had all different names such as chkDog, chkCat, chkFish, etc would the code need to be change to reflect that?
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
We cannot see your database and can only go on the info you provide.
Yes you do need to update the code to match your checkbox names.
If they are called individual names then jack's code will be better.
 

GrahamUK33

Registered User.
Local time
Today, 03:33
Joined
May 19, 2011
Messages
58
The following code does work when selected, but if selected again it doesn't uncheck the tickboxes.

Code:
Private Sub lblTitle_Click()
  Me.chkCheckBox1.Value = True
  Me.chkCheckBox2.Value = True
  Me.chkCheckBox3.Value = True
  Me.chkCheckBox4.Value = True
  Me.chkCheckBox5.Value = True
End Sub
 

jleach

Registered User.
Local time
Yesterday, 22:33
Joined
Jan 4, 2012
Messages
308
>> Microsoft Access can't find the field 'chkCheckBox1' referred to in your expression. <<

Be explicit:

Code:
Me.Controls("chkCheckBox" & Trim(Str(i))).Value = True

(Colin's code is looking for Fields, as that's the default collection of Me... instead reference the Controls collection explicitly. I also prefer explicit trimming and conversion of the integer value, to ensure there's no leading spaces that sometimes occur)

To deselect, you can revert the value (toggle based off the current state of the checkbox):

Code:
Private Sub lblTitle_Click()
  Me.chkCheckBox1.Value = NOT Nz(Me.chkCheckBox1.Value, False)
  Me.chkCheckBox2.Value = NOT Nz(Me.chkCheckBox2.Value, False)
  Me.chkCheckBox3.Value = NOT Nz(Me.chkCheckBox3.Value, False)
  Me.chkCheckBox4.Value = NOT Nz(Me.chkCheckBox4.Value, False)
  Me.chkCheckBox5.Value = NOT Nz(Me.chkCheckBox5.Value, False)
End Sub

However, note that this will swap each control's value, so if some are checked and some aren't, it will inverse those. If you want to have "make all true" or "make all false" regardless of their current state, you will need separate labels/commands to force that (like a Select All and Deselect All command/label).

For Colin's method, use this:

Code:
Me.Controls("chkCheckBox" & Trim(Str(i))).Value = NOT Nz(Me.Controls("chkCheckbox" & Trim(Str(i))).Value, False)

hth
 

Minty

AWF VIP
Local time
Today, 03:33
Joined
Jul 26, 2013
Messages
10,372
If you want to toggle them you'll need another piece of code , toggling the existing value won't reset them all. Obviously the code is only currently setting them to true.

I would simply add another set of code to the double click event.
Code:
Private Sub lblTitle_DoubleClick()
  Me.chkCheckBox1.Value = False
  Me.chkCheckBox2.Value = False
  Me.chkCheckBox3.Value = False
  Me.chkCheckBox4.Value = False
  Me.chkCheckBox5.Value = False
End Sub
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 03:33
Joined
Jul 9, 2003
Messages
16,331
.Value NOT required as its the default

This is true, BUT be careful, in some circumstances you can reference the actual control as an object which causes an error.

I've seen this when converting Macros to VBA.


Sent from my SM-G925F using Tapatalk
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
I often have trouble with getting separate click & double click event code to fire reliably. Sometime a user double clicks by mistake etc ....

If you want to toggle all checkboxes true or false then I would change the label caption and do one of the following:

Code:
Private Sub lblTitle_Click()

Dim I As Integer

If Me.lblTitle.Caption="Set Checkboxes True" Then
[INDENT]  Me.lblTitle.Caption="Set Checkboxes False"
  For I = 1 to 5
      Me("chkCheckBox" & I) = True
  Next[/INDENT]
Else
[INDENT]  Me.lblTitle.Caption="Set Checkboxes True"
  For I = 1 to 5
      Me("chkCheckBox" & I) = False
  Next[/INDENT]
End If

End Sub

OR

Code:
Private Sub lblTitle_Click()

Dim I As Integer

If Me.lblTitle.Caption="Set Checkboxes True" Then
[INDENT]  Me.lblTitle.Caption="Set Checkboxes False"
  Me.chkCheckBox1.Value = True
  Me.chkCheckBox2.Value = True
  Me.chkCheckBox3.Value = True
  Me.chkCheckBox4.Value = True
  Me.chkCheckBox5.Value = True[/INDENT]
Else
[INDENT]  Me.lblTitle.Caption="Set Checkboxes True"
  Me.chkCheckBox1.Value = False
  Me.chkCheckBox2.Value = False
  Me.chkCheckBox3.Value = False
  Me.chkCheckBox4.Value = False
  Me.chkCheckBox5.Value = False[/INDENT]
End If

End Sub

So you now have 3 approaches to choose from ...
Please don't combine them again :D

As for the .Value issue raised by Jack & now by Tony, I never have issues with omitting it but perhaps that's due to the way I write my code.
I NEVER use macros so never need to convert to VBA. It certainly won't do any harm to include .Value
 

GrahamUK33

Registered User.
Local time
Today, 03:33
Joined
May 19, 2011
Messages
58
Thanks so much for all your help, I decided not to use the toggle option, I thought that may be a bit confusing for the user. The click and double click options has worked for me, and less confusing for the user.


Code:
Private Sub lblTitle_Click()
  Me.chkCheckBox1.Value = True 
  Me.chkCheckBox2.Value = True 
  Me.chkCheckBox3.Value = True 
  Me.chkCheckBox4.Value = True 
  Me.chkCheckBox5.Value = True 
End Sub 

Private Sub lblTitle_DoubleClick() 
  Me.chkCheckBox1.Value = False 
  Me.chkCheckBox2.Value = False 
  Me.chkCheckBox3.Value = False 
  Me.chkCheckBox4.Value = False 
  Me.chkCheckBox5.Value = False 
End Sub
 

isladogs

MVP / VIP
Local time
Today, 03:33
Joined
Jan 14, 2017
Messages
18,258
Arrggghhh - Think of the RSI !!!
LOL - I didn't say I was going to start using it myself!

I decided not to use the toggle option, I thought that may be a bit confusing for the user. The click and double click options has worked for me, and less confusing for the user.

Use whatever works for you.
However, for info, I abandoned that approach years ago based on user feedback.
The consensus was they hated separate single & double click events on the same control
 

Users who are viewing this thread

Top Bottom