By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,830 Members | 682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,830 IT Pros & Developers. It's quick & easy.

CheckBoxALL to Auto Check 52 other checkboxes Loop Question

kcdoell
100+
P: 230
I have a list of states on a form, in which the user can click on various states that are applicable to the form. There is a "ALL State" option (CheckBoxALL) in which if selected all of the state checkboxes will be checked. Below is the following code:



Private Sub CheckBoxALL_Click()



If CheckBoxAll = True Then

CheckBoxState01 = True
CheckBoxState02 = True
CheckBoxState03 = True
CheckBoxState04 = True
etc.....
CheckBoxState52 = True

Else

CheckBoxState01 = False
CheckBoxState02 = False
CheckBoxState03 = False
CheckBoxState04 = False
CheckBoxState05 = False
CheckBoxState06 = False
etc.........CheckBoxState52 = False

End If

End Sub

_______________________

My question is if there is a cleaner way to write this code than the way I wrote it since I am still in VBA learning mode?

I tried something like this but it keeps error out:

Private Sub CheckBoxALL_Click()

Dim CheckALL As Boolean

Set CheckALL = ActiveDocument.FormFields("CheckBoxALL").CheckBox. Value

If CheckALL("CheckBoxALL").CheckBox.Value = True Then

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = True
Next i

Else

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = False
Next i

End If
End Sub

_________________


My result so far has been compile errors and coding that does not work. Does anybody have any ideas??

Thanks,



Keith.
Jan 14 '08 #1
Share this Question
Share on Google+
13 Replies


FishVal
Expert 2.5K+
P: 2,653
Hi, Keith.

You may use the following code.
Expand|Select|Wrap|Line Numbers
  1. For i = 1 To 52
  2.     Me.Controls("CheckBox" & Format(i, "00")).Value = True
  3. Next i
  4.  
Just out of curiosity. Does that mean you have a table with 52 Yes/No fields?

Regards,
Fish
Jan 14 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_AfterUpdate()
  2.  
  3. Dim ctl As Control
  4.  
  5. If Me.CheckBoxAll = -1 Then
  6.  
  7.  For Each ctl In Me.Controls
  8.  
  9.    If ctl.ControlType = acCheckBox Then
  10.  
  11.      ctl = -1
  12.  
  13.    End If
  14. Next
  15.  
  16. Else
  17.  
  18. If Me.CheckBoxAll = -1 Then
  19.  
  20.  For Each ctl In Me.Controls
  21.  
  22.    If ctl.ControlType = acCheckBox Then
  23.  
  24.      ctl = 0
  25.  
  26.   End If
  27.   Next
  28. End If
  29. End Sub
Linq ;0)>
Jan 14 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxState" & i so

Expand|Select|Wrap|Line Numbers
  1. Me.Controls("CheckBoxState" & Format(i, "00")).Value = True
Linq ;0)>
Jan 14 '08 #4

FishVal
Expert 2.5K+
P: 2,653
Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxState" & i so

Expand|Select|Wrap|Line Numbers
  1. Me.Controls("CheckBoxState" & Format(i, "00")).Value = True
Linq ;0)>
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_AfterUpdate()
  2.  
  3.     Dim ctl As Control
  4.  
  5.     With Me
  6.         For Each ctl In .Controls
  7.            If ctl.ControlType = acCheckBox Then ctl = .CheckBoxAll
  8.         Next
  9.     End With
  10.  
  11.     Set ctl = Nothing
  12.  
  13. End Sub
  14.  
Eye for eye, Linq. LOL.

Best regards,
Fish
Jan 14 '08 #5

kcdoell
100+
P: 230
Thanks for all the input... I just go back out of a meeting....


This is a form with a section in it for people to pick (CheckBoxes) the applicable state(s). I failed to mention that it was a form created out of Word 2003. That seems to make a difference because it does not recognize the command "Controls" (VBA error message:"Method or data member not found"). Does that mean that it can not be done if this form was created in Word or I would have to use some other command.....

Thanks,

Keith.
Jan 14 '08 #6

FishVal
Expert 2.5K+
P: 2,653
Ok, Keith.

Try the following code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_Click()
  2.  
  3.     Dim fldField As Field
  4.  
  5.     For Each fldField In ThisDocument.Fields
  6.         If TypeName(fldField.OLEFormat.Object) = "CheckBox" Then _
  7.             fldField.OLEFormat.Object.Value = ThisDocument.CheckBoxAll
  8.     Next
  9.  
  10.     Set fldField = Nothing
  11.  
  12. End Sub
  13.  
Jan 14 '08 #7

kcdoell
100+
P: 230
Hello:

I tried that and got the following error:

Run time 91 "Object Variable or with Block Variable not Set"

I don't really understand what the code is doing to interpret the error message. Any ideas?

Thanks,

Keith
Jan 14 '08 #8

FishVal
Expert 2.5K+
P: 2,653
Well. You may send me it via email. I will PM you my email address.
Jan 15 '08 #9

kcdoell
100+
P: 230
Well. You may send me it via email. I will PM you my email address.

Fish did you get it?? I sent it to you yesterday.

Thank,

Keith.
Jan 16 '08 #10

FishVal
Expert 2.5K+
P: 2,653
Fish did you get it?? I sent it to you yesterday.

Thank,

Keith.
Hello, Keith.

Sorry for delay. I've send you a working copy of your file. The problem wasn't in coding but in the file itself - it was stucked in design mode in a hopeless try to create a control. So I've just copied the table to an empty word document and added the code posted above. Looks like it is working.

Regards,
Fish
Jan 16 '08 #11

kcdoell
100+
P: 230
Thanks Fish for all your help. I thought something fishy was going on no pun intended.

Thanks again :-)

Keith.
Jan 16 '08 #12

P: 1
I have a list of states on a form, in which the user can click on various states that are applicable to the form. There is a "ALL State" option (CheckBoxALL) in which if selected all of the state checkboxes will be checked. Below is the following code:



Private Sub CheckBoxALL_Click()



If CheckBoxAll = True Then

CheckBoxState01 = True
CheckBoxState02 = True
CheckBoxState03 = True
CheckBoxState04 = True
etc.....
CheckBoxState52 = True

Else

CheckBoxState01 = False
CheckBoxState02 = False
CheckBoxState03 = False
CheckBoxState04 = False
CheckBoxState05 = False
CheckBoxState06 = False
etc.........CheckBoxState52 = False

End If

End Sub

_______________________

My question is if there is a cleaner way to write this code than the way I wrote it since I am still in VBA learning mode?

I tried something like this but it keeps error out:

Private Sub CheckBoxALL_Click()

Dim CheckALL As Boolean

Set CheckALL = ActiveDocument.FormFields("CheckBoxALL").CheckBox. Value

If CheckALL("CheckBoxALL").CheckBox.Value = True Then

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = True
Next i

Else

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = False
Next i

End If
End Sub

_________________


My result so far has been compile errors and coding that does not work. Does anybody have any ideas??

Thanks,



Keith.
Hi Keith! Can I ask you something please?
Is the first code you suggested right??
Because I have only 5 checkboxes and it seems easyer to use it...

Thanks
maria
Jan 26 '08 #13

missinglinq
Expert 2.5K+
P: 3,532
Yes, Maria! This will work fine:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxALL_Click()
  2.  
  3. If CheckBoxAll = True Then
  4.  
  5. CheckBox1 = True
  6. CheckBox2 = True
  7. CheckBox3 = True
  8. CheckBox4 = True
  9. CheckBox5 = True
  10.  
  11. Else
  12.  
  13. CheckBox1 = False
  14. CheckBox2 = False
  15. CheckBox3 = False
  16. CheckBox4 = False
  17. CheckBox5 = False
  18.  
  19. End If
  20.  
  21. End Sub
  22.  
You just wouldn't want to have to do all this for 50+ checkboxes!


Welcome to TheScripts!

Linq ;0)>
Jan 26 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.