check-boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • questionit
    Contributor
    • Feb 2007
    • 553

    check-boxes

    If you have so many check-boxes on a form .

    If you want to see if atleast 2 of them have been ticked. Do you have to loop through each of it - it would be difficult since each check-box has a different name so an array of string would have to be created - and that it will be a long procedure.

    Is there anything simpler method ?

    Thanks
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi
    Originally posted by questionit
    If you have so many check-boxes on a form .

    If you want to see if atleast 2 of them have been ticked. Do you have to loop through each of it - it would be difficult since each check-box has a different name so an array of string would have to be created - and that it will be a long procedure.

    Is there anything simpler method ?

    Thanks
    In short Yes

    Try looking at
    Code:
    dim Ctrl as Control
    For Ech Ctrl in Me.Controls
         if ctrl.ControlType=acCheckBox and ctrl=True then
            ' Your code logic here
        end if
    next ctrl
    ??

    MTB

    Comment

    • questionit
      Contributor
      • Feb 2007
      • 553

      #3
      MTB

      I havn't understood the login of your code.

      If i have to loop through all the Controls manually anyway , whats the need of the code you have just given?

      your code is checking if all the controls on Me. are true (means ticked?) then do something - is that right?

      can i not just get the count of how many are ticked by doing a simple For loop?




      Originally posted by MikeTheBike
      Hi

      In short Yes

      Try looking at
      Code:
      dim Ctrl as Control
      For Ech Ctrl in Me.Controls
           if ctrl.ControlType=acCheckBox and ctrl=True then
              ' Your code logic here
          end if
      next ctrl
      ??

      MTB

      Comment

      • MikeTheBike
        Recognized Expert Contributor
        • Jun 2007
        • 640

        #4
        Originally posted by questionit
        MTB

        I havn't understood the login of your code.

        If i have to loop through all the Controls manually anyway , whats the need of the code you have just given?

        your code is checking if all the controls on Me. are true (means ticked?) then do something - is that right?

        can i not just get the count of how many are ticked by doing a simple For loop?
        Yes, and anything else you like !

        ie. you need to set up a counter and (perhaps) exit the for loop ('Exit For' will do that) if it becomes 2 or whatever ??

        MTB

        Comment

        • questionit
          Contributor
          • Feb 2007
          • 553

          #5
          MTB

          Can you help me in starting setting up the counter?

          Thanks

          Originally posted by MikeTheBike
          Yes, and anything else you like !

          ie. you need to set up a counter and (perhaps) exit the for loop ('Exit For' will do that) if it becomes 2 or whatever ??

          MTB

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            [CODE=vb]Dim Ctrl as Control
            Dim CBCounter as Integer
            CBCounter = 0

            For Each Ctrl in Me.Controls
            If ctrl.ControlTyp e=acCheckBox and ctrl=True then
            CBCounter = CBCounter + 1
            End If
            Next ctrl

            If CBCounter > 1 Then
            'Do whatever you need to do if condition is met
            End If
            [/CODE]

            Comment

            • MikeTheBike
              Recognized Expert Contributor
              • Jun 2007
              • 640

              #7
              Hi missinglinq

              I was hoping that questionit might try having a go, its the only way to learn !?

              MTB

              Comment

              • questionit
                Contributor
                • Feb 2007
                • 553

                #8
                HI MTB /Missinglinq

                I get error on this:

                If Ctrl.ControlTyp e = acCheckBox And Ctrl = True Then

                "Object doesn't support this property or method. "

                I am using VBA, which doesn't have .ControlType property i think -- any alternatives?




                Originally posted by MikeTheBike
                Hi missinglinq

                I was hoping that questionit might try having a go, its the only way to learn !?

                MTB

                Comment

                • missinglinq
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3533

                  #9
                  Try this:
                  [CODE=vb]Dim Ctrl as Control
                  Dim CBCounter as Integer
                  CBCounter = 0
                  For Each Ctrl in Me.Controls
                  If TypeOf ctrl Is CheckBox and ctrl = True Then
                  CBCounter = CBCounter + 1
                  End If
                  Next ctrl
                  If CBCounter > 1 Then
                  'Do whatever you need to do if condition is met
                  End If
                  [/CODE]

                  Comment

                  • MikeTheBike
                    Recognized Expert Contributor
                    • Jun 2007
                    • 640

                    #10
                    Originally posted by missinglinq
                    Try this:
                    [CODE=vb]Dim Ctrl as Control
                    Dim CBCounter as Integer
                    CBCounter = 0
                    For Each Ctrl in Me.Controls
                    If TypeOf ctrl Is CheckBox and ctrl = True Then
                    CBCounter = CBCounter + 1
                    End If
                    Next ctrl
                    If CBCounter > 1 Then
                    'Do whatever you need to do if condition is met
                    End If
                    [/CODE]
                    Or pehaps this
                    [CODE=vb]Dim Ctrl as Control
                    Dim CBCounter as Integer
                    CBCounter = 0
                    For Each Ctrl in Me.Controls
                    If Ctrl.ControlTyp e = acCheckBox Then
                    If Ctrl = True Then CBCounter = CBCounter + 1
                    End If
                    Next ctrl
                    If CBCounter > 1 Then
                    'Do whatever you need to do if condition is met
                    End If
                    [/CODE]

                    ??

                    MTB

                    Comment

                    • missinglinq
                      Recognized Expert Specialist
                      • Nov 2006
                      • 3533

                      #11
                      It's the little things that trip you up! I copied Mike's original code and modified it, and he did the same thing. The problem was, he had a typo in it!

                      dim Ctrl as Control
                      For Ech Ctrl in Me.Controls
                      if ctrl.ControlTyp e=acCheckBox and ctrl=True then
                      ' Your code logic here
                      end if
                      next ctrl

                      Ech should have been Each! I've corrected this in all the code listed here except the original posting by Mike, and I expect both versions will now work! I haven't tried the ControlType but I expect that works just fine. There's about three or four ways of referencing the control types, all of which are fine!

                      The hilited line on the error
                      If Ctrl.ControlTyp e = acCheckBox And Ctrl = True Then
                      points out a common flaw in the VB Editor; it tends to hilite the next line after the line that causes the error. In this case that was the line:
                      For Ech Ctrl in Me.Controls

                      Linq ;0)>

                      Comment

                      • questionit
                        Contributor
                        • Feb 2007
                        • 553

                        #12
                        Thanks missinglinq

                        I've got the things working fine now with the line from the previous code posting:
                        Code:
                         If Ctrl.ControlType = acCheckBox Then
                        But I get error if i do it this way :
                        If TypeOf Ctrl Is CheckBox And Ctrl = True Then
                        Error: "Object doesn't support the property or method"

                        - probably this one is not supported by VBA :)

                        Originally posted by missinglinq
                        It's the little things that trip you up! I copied Mike's original code and modified it, and he did the same thing. The problem was, he had a typo in it!

                        dim Ctrl as Control
                        For Ech Ctrl in Me.Controls
                        if ctrl.ControlTyp e=acCheckBox and ctrl=True then
                        ' Your code logic here
                        end if
                        next ctrl

                        Ech should have been Each! I've corrected this in all the code listed here except the original posting by Mike, and I expect both versions will now work! I haven't tried the ControlType but I expect that works just fine. There's about three or four ways of referencing the control types, all of which are fine!

                        The hilited line on the error
                        If Ctrl.ControlTyp e = acCheckBox And Ctrl = True Then
                        points out a common flaw in the VB Editor; it tends to hilite the next line after the line that causes the error. In this case that was the line:
                        For Ech Ctrl in Me.Controls

                        Linq ;0)>

                        Comment

                        • questionit
                          Contributor
                          • Feb 2007
                          • 553

                          #13
                          Hey Experts

                          By the way

                          By your method i.e
                          Code:
                          For Each Ctrl In Me.Controls 
                          .......
                          We check every check-box on the form. But what if we dont want to check all of them. For example i have a few sets of check-box on a form and i want to look for those only which i am interested in ? Sounds impossible task to me !

                          Originally posted by questionit
                          Thanks missinglinq

                          I've got the things working fine now with the line from the previous code posting:
                          Code:
                           If Ctrl.ControlType = acCheckBox Then
                          But I get error if i do it this way :
                          If TypeOf Ctrl Is CheckBox And Ctrl = True Then
                          Error: "Object doesn't support the property or method"

                          - probably this one is not supported by VBA :)

                          Comment

                          • missinglinq
                            Recognized Expert Specialist
                            • Nov 2006
                            • 3533

                            #14
                            No, it's not impossible at all! I'll be away from my PC for a while, but if you'll post the actual code you're now using, when I get back I'll show you how to only check certain controls! It's really an easy technique!

                            Linq ;0)>

                            Comment

                            • MikeTheBike
                              Recognized Expert Contributor
                              • Jun 2007
                              • 640

                              #15
                              Hi
                              For Ech Ctrl in Me.Controls
                              Good spot missinglinq!

                              As for
                              I've got the things working fine now with the line from the previous code posting:

                              Code: ( text )
                              1. If Ctrl.ControlTyp e = acCheckBox Then


                              But I get error if i do it this way :
                              If TypeOf Ctrl Is CheckBox And Ctrl = True Then
                              Error: "Object doesn't support the property or method"

                              - probably this one is not supported by VBA :)
                              I believe that this error/solution
                              I've got the things working fine now with the line from the previous code posting:

                              Code: ( text )
                              1. If Ctrl.ControlTyp e = acCheckBox Then


                              But I get error if i do it this way :
                              If TypeOf Ctrl Is CheckBox And Ctrl = True Then
                              Error: "Object doesn't support the property or method"

                              - probably this one is not supported by VBA :)
                              is due to some of the controls not liking ctrl=True (a lable for instance)
                              which is why I changed it to be used only if the control is a checkbox !


                              I cannot think of away (off hand) to check specific checkboxes, other than the obviouse of using the checkbox names.
                              This is possible with a little mod to the code, if you want to go this way, but it will mean hard coding the checkbox names into the code, but this is not particularly desirable for design maintenance.


                              MTB

                              Comment

                              Working...