Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button to check all checkboxes on form 2

Status
Not open for further replies.

xollef

Technical User
Apr 23, 2002
30
I have a subform or will have actually, on it there is listed approximately 10 checkboxes. I want a button that when pressed all checkboxes get checked and the opposite when pressed again all checkboxes gets unchecked.It seems like it should be easy but I am stuck anyway.
 
If I were doing this, I would make an 11th checkbox that would be labled as "Check All". I would name each checkbox object with a name like "chkbxN", where N equals a number from 1 to 10. On the 11th checkbox, we'll call it chkbxAll, we'll put the following function on the OnClick event

Private Sub chkbxAll_Click()
Dim bolState As Boolean

If chkbxAll.Value = True Then
bolState = True
Else
bolState = False
End If

For i = 1 To 10
Me.Controls("chkbx" & i).Value = bolState
Next i

End Sub petersdaniel@hotmail.com
"If A equals success, then the formula is: A=X+Y+Z. X is work. Y is play. Z is keep your mouth shut." --Albert Einstein

 
Thank you It will come in handy first thing in the morning.
 
Hi!

Another way to do this like this, make the caption of the button Check All and then use this code:

Dim cntl As Control
Dim bolCheck As Boolean

If Me!YourButton.Name = "Check All" Then
bolCheck = True
Else
bolCheck = False
End If

For Each cntl In Me.Controls
If cntl.ControlType = acCheckBox Then
Me.Controls(cntl.Name).Value = bolCheck
End If
Next cntl

If bolCheck = True Then
Me!YourButton.Caption = "UnCheck All"
Else
Me!YourButton.Caption = "Check All"
End If

You will need to use the Form's Current event to make sure the button has the proper caption.

hth
Jeff Bridgham
bridgham@purdue.edu
 
The only problem with this suggestion is that it universally checks or unchecks all check boxes on the form. If you want to have one set of checkboxes with a consolidated control and have others that are not influenced by it, this method of enumeration through all checkboxes on the form would be to genralized. The previous suggestion keeps control within the bounds of a spcific group of controls.

This is only an issue if you have the situation where other check controls may be present petersdaniel@hotmail.com
"If A equals success, then the formula is: A=X+Y+Z. X is work. Y is play. Z is keep your mouth shut." --Albert Einstein

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top