CheckBoxALL to Auto Check 52 other checkboxes Loop Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kcdoell
    New Member
    • Dec 2007
    • 230

    CheckBoxALL to Auto Check 52 other checkboxes Loop Question

    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_Cli ck()



    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.........Che ckBoxState52 = 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_Cli ck()

    Dim CheckALL As Boolean

    Set CheckALL = ActiveDocument. FormFields("Che ckBoxALL").Chec kBox.Value

    If CheckALL("Check BoxALL").CheckB ox.Value = True Then

    For i = 1 To 52
    CheckALL("Check BoxState0" & i).CheckBox.Val ue = True
    Next i

    Else

    For i = 1 To 52
    CheckALL("Check BoxState0" & i).CheckBox.Val ue = 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.
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, Keith.

    You may use the following code.
    [code=vb]
    For i = 1 To 52
    Me.Controls("Ch eckBox" & Format(i, "00")).Valu e = True
    Next i
    [/code]

    Just out of curiosity. Does that mean you have a table with 52 Yes/No fields?

    Regards,
    Fish

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      [CODE=vb]Private Sub CheckBoxAll_Aft erUpdate()

      Dim ctl As Control

      If Me.CheckBoxAll = -1 Then

      For Each ctl In Me.Controls

      If ctl.ControlType = acCheckBox Then

      ctl = -1

      End If
      Next

      Else

      If Me.CheckBoxAll = -1 Then

      For Each ctl In Me.Controls

      If ctl.ControlType = acCheckBox Then

      ctl = 0

      End If
      Next
      End If
      End Sub[/CODE]

      Linq ;0)>

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxSt ate" & i so

        [CODE=vb]Me.Controls("Ch eckBoxState" & Format(i, "00")).Valu e = True[/CODE]
        Linq ;0)>

        Comment

        • FishVal
          Recognized Expert Specialist
          • Jun 2007
          • 2656

          #5
          Originally posted by missinglinq
          Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxSt ate" & i so

          [CODE=vb]Me.Controls("Ch eckBoxState" & Format(i, "00")).Valu e = True[/CODE]
          Linq ;0)>
          [code=vb]
          Private Sub CheckBoxAll_Aft erUpdate()

          Dim ctl As Control

          With Me
          For Each ctl In .Controls
          If ctl.ControlType = acCheckBox Then ctl = .CheckBoxAll
          Next
          End With

          Set ctl = Nothing

          End Sub
          [/code]

          Eye for eye, Linq. LOL.

          Best regards,
          Fish

          Comment

          • kcdoell
            New Member
            • Dec 2007
            • 230

            #6
            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.

            Comment

            • FishVal
              Recognized Expert Specialist
              • Jun 2007
              • 2656

              #7
              Ok, Keith.

              Try the following code.
              [code=vb]
              Private Sub CheckBoxAll_Cli ck()

              Dim fldField As Field

              For Each fldField In ThisDocument.Fi elds
              If TypeName(fldFie ld.OLEFormat.Ob ject) = "CheckBox" Then _
              fldField.OLEFor mat.Object.Valu e = ThisDocument.Ch eckBoxAll
              Next

              Set fldField = Nothing

              End Sub
              [/code]

              Comment

              • kcdoell
                New Member
                • Dec 2007
                • 230

                #8
                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

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  Well. You may send me it via email. I will PM you my email address.

                  Comment

                  • kcdoell
                    New Member
                    • Dec 2007
                    • 230

                    #10
                    Originally posted by FishVal
                    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.

                    Comment

                    • FishVal
                      Recognized Expert Specialist
                      • Jun 2007
                      • 2656

                      #11
                      Originally posted by kcdoell
                      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

                      Comment

                      • kcdoell
                        New Member
                        • Dec 2007
                        • 230

                        #12
                        Thanks Fish for all your help. I thought something fishy was going on no pun intended.

                        Thanks again :-)

                        Keith.

                        Comment

                        • maria9cy
                          New Member
                          • Jan 2008
                          • 1

                          #13
                          Originally posted by kcdoell
                          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_Cli ck()



                          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.........Che ckBoxState52 = 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_Cli ck()

                          Dim CheckALL As Boolean

                          Set CheckALL = ActiveDocument. FormFields("Che ckBoxALL").Chec kBox.Value

                          If CheckALL("Check BoxALL").CheckB ox.Value = True Then

                          For i = 1 To 52
                          CheckALL("Check BoxState0" & i).CheckBox.Val ue = True
                          Next i

                          Else

                          For i = 1 To 52
                          CheckALL("Check BoxState0" & i).CheckBox.Val ue = 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

                          Comment

                          • missinglinq
                            Recognized Expert Specialist
                            • Nov 2006
                            • 3533

                            #14
                            Yes, Maria! This will work fine:

                            [CODE=vb]Private Sub CheckBoxALL_Cli ck()

                            If CheckBoxAll = True Then

                            CheckBox1 = True
                            CheckBox2 = True
                            CheckBox3 = True
                            CheckBox4 = True
                            CheckBox5 = True

                            Else

                            CheckBox1 = False
                            CheckBox2 = False
                            CheckBox3 = False
                            CheckBox4 = False
                            CheckBox5 = False

                            End If

                            End Sub
                            [/CODE] You just wouldn't want to have to do all this for 50+ checkboxes!


                            Welcome to TheScripts!

                            Linq ;0)>

                            Comment

                            Working...