Checkbox selects other Checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beacon
    Contributor
    • Aug 2007
    • 579

    Checkbox selects other Checkboxes

    Hi everybody,

    [Access 2003]

    I have a quality assurance database that has a form with a bunch of checkboxes on it that serve as reasons for why a product didn't pass inspection.

    It's a given that the users will be able to select individual checkboxes to indicate the reasons. What I would like to find out how to do is to put a checkbox or radio button on the form that will select all the checkboxes if it's selected.

    When I added a checkbox to my form and tried to write code for it, the control didn't have a 'Value' or 'Checked' property for me to choose. Is it not possible to do this? If I use a radio button, I'd like to have one to 'Select All' and one to 'Deselect' if at all possible.

    I tried the following to no avail based on something I found online:
    Code:
    Private Sub chkSelectAll_AfterUpdate()
    me.chkDefect = me.chkSelectAll
    me.chkPowerSupply = me.chkSelectAll
    end sub
    Can anyone share a little help with me?

    Thanks in advance,
    beacon
  • OldBirdman
    Contributor
    • Mar 2007
    • 675

    #2
    Code:
    me!chkDefect = me!chkSelectAll
    me!chkPowerSupply = me!chkSelectAll
    You do not need a 'Deselect' as all will be deselected if/when chkSelectAll is clicked again, and is then false.
    It makes no difference whether you use checkboxes or radiobuttons. However, radiobuttons imply that only one can be true at any given time. Radio button refers to a radio presets, and selecting a station deselects another. I assume that a product can fail for multiple reasons simultaneously, and therefore checkboxes would be my preference.

    OldBirdman

    Comment

    • beacon
      Contributor
      • Aug 2007
      • 579

      #3
      Hi OldBirdMan,

      Here's the code I entered for my 'Select All' checkbox. I don't think it matters, but the checkboxes are all on a tab control.

      Code:
      Private Sub chkSelectAll_AfterUpdate()
      me.chkDefect = me.chkSelectAll
      End Sub
      When I enter this and go back to use the 'Select All' checkbox, I get an error message that says, "Run-time error 438: Object doesn't support this method or property."

      This is the error message I was getting initially that prompted me to post to the forum. I don't use checkboxes that often and this is the first time I've tried to control other checkboxes with a checkbox.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Hi OB. Good to see you around again :)

        Assuming the clever naming structure that OB displays in his example (All CheckBox controls, and only CheckBox controls, start chk) and also that chkSelectAll & chkDeselectAll are the only ones ending in the last three characters "All", then the following code should help somewhat :
        Code:
        Private Sub chkSelectAll_AfterUpdate()
          Call SetAll(blnValue:=True
        End Sub
        
        Private Sub chkDeselectAll_AfterUpdate()
          Call SetAll(blnValue:=False
        End Sub
        
        Private Sub SetAll(blnValue As Boolean)
          Dim ctlThis As Form.Control
        
          For Each ctlThis In Me.Controls
            If Left(ctlThis.Name, 3) = "chk" _
            And Right(ctlThis.Name, 3) <> "All" Then _
              ctlThis = blnValue
          Next ctlThis
        End Sub

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Originally posted by beacon
          Code:
          Private Sub chkSelectAll_AfterUpdate()
          me.chkDefect = me.chkSelectAll
          End Sub
          When I enter this and go back to use the 'Select All' checkbox, I get an error message that says, "Run-time error 438: Object doesn't support this method or property."
          This is because the items you're dealing with are objects whose default property is .Value.

          When mixed with other items (EG. Me.chkDefect = blnValue) it determines the default property to be required. When used object to object (as here) it thinks you want to assign one object to the other (EG. Set objVar = Me.chkSelectAll ).

          Does that clarify your understanding?

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Originally posted by beacon
            ...
            Code:
            Private Sub chkSelectAll_AfterUpdate()
            me.chkDefect = me.chkSelectAll
            End Sub
            When I enter this and go back to use the 'Select All' checkbox, I get an error message that says, "Run-time error 438: Object doesn't support this method or property."
            ....
            That only means Me object doesn't support either chkDefect or chkSelectAll property, most probably chkDefect.

            Comment

            • beacon
              Contributor
              • Aug 2007
              • 579

              #7
              Originally posted by NeoPa
              This is because the items you're dealing with are objects whose default property is .Value.

              When mixed with other items (EG. Me.chkDefect = blnValue) it determines the default property to be required. When used object to object (as here) it thinks you want to assign one object to the other (EG. Set objVar = Me.chkSelectAll ).

              Does that clarify your understanding?
              I understand what you're saying, but I'm not sure how to rectify the problem. I saw your response that showed how I could loop through each of the controls based on the 'chk' prefix, but it brings up another potential issue I'll undoubtedly run into.

              The potential issue is how can I handle this problem if I'm asked to put in another checkbox that allows the user to select default reasons. For instance, we have 'Select All' that chooses all of the checkboxes and we have a 'Select Default' that only chooses a certain selection based on the product type...how can I indicate just a select few to be selected when that option is chosen?

              Keep in mind that there are multiple different products so the options will be different for 'Select Default' based on the product.

              Comment

              • OldBirdman
                Contributor
                • Mar 2007
                • 675

                #8
                No, it doesn't clear up anything. It seems like I assign .value to .value all the time. txtA = txtB, chkC = chkD, or txtE = cboF, etc. Never had any issues here with Access making the assignments. Because this code is in the form's module, can't the "me" be dropped completely?

                Clever naming structure belongs to beacon's original post, I kept answers in his framework, but it is similar to my naming conventions.

                As to having a "DeSelectAl l" checkbox, the code shown does not clear it when "SelectAll" is checked. Reflecting on this, I don't think there should be either a Select or DeSelect checkbox. They should be command buttons, "Select All" and "Clear All".

                If a product is defective in all categories except 1, user might want to select all, and then deselect it (i.e. uncheck chkPowerSupply) to show that it passed the one test. The chkSelectAll is now incorrect if it means that all checkboxes are checked. It gets more complicated because, after each individual checkbox is changed, all must be checked to see if either chkSelectAll or chkDeSelectAll should be changed. chkSelectAll = (chkPowerSupply And chkDefect . . .) and DeSelectAll = Not (chkPowerSupply Or chkDefect Or . . .). This could be done in a loop similar to that shown in Post #4 by NeoPa.

                This could be expanded to include a "Default" command button. (This discussion is now about sets of checkboxes, not the actual problem presented, as something is very wrong if there is a "Default" set of flaws in a product. Improve your quality control, or get a new supplier). Each item would have a Default set of flaws.

                Checkboxes would have to be TripleState, and locked if grayed, or Checkboxes would vary from item to item. You might consider a subform with each record showing an inspection item and a checkbox. This is a database design issue.

                OldBirdman

                Comment

                • beacon
                  Contributor
                  • Aug 2007
                  • 579

                  #9
                  I think I found my answer on this website: ** Edit ** link removed.

                  In case you don't want to go to the website, the gist of the solution is to enter a tag for each of the checkboxes I want selected (All or default) and then create an AfterUpdate() event for the checkbox that will trigger the event.

                  Here's the code assuming that I put a tag of 'SelectAll' or 'Default' in for the checkboxes that I want controlled by the chkSelectAll or chkDefault:
                  Code:
                  Private Sub chkSelectAll_AfterUpdate()
                      Dim ctl as Control
                   
                      For Each ctl In Me.Controls
                          If Me.chkSelectAll = True Then
                              If ctl.Tag = "SelectAll" Then
                                  ctl.Value = True
                              End If
                          Else
                              If ctl.Tag = "SelectAll" Then
                                  ctl.Value = False
                              End If
                          End If
                      Next ctl
                  End Sub
                  This will check only the boxes that you've entered a tag for. I took the code I found and took it one step further by adding the conditional "If Me.chkSelectAll = True" around the other conditional "If ctl.Tag = 'SelectAll'..." which allows you to deselect your selection.

                  I don't really understand how I was able to give a ctl a .Value when it wasn't available from the properties when I typed in the code, but it works. I know that you can't just type the following:
                  Code:
                  If ctl.Tag = "SelectAll" Then
                       ctl.Value = True
                  Else
                       ctl.Value = False
                  End If
                  ...because it will give you an error message similar to the runtime error 438 I was receiving when I initially began this process.

                  You have to put in some work to add the tags to each of the checkboxes, but once you get that entered it works pretty well.

                  Thanks for everybody's input...
                  Last edited by NeoPa; Jan 21 '09, 10:39 PM. Reason: Removed link to competing forum.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    Originally posted by beacon
                    I understand what you're saying, but I'm not sure how to rectify the problem. I saw your response that showed how I could loop through each of the controls based on the 'chk' prefix, but it brings up another potential issue I'll undoubtedly run into.

                    The potential issue is how can I handle this problem if I'm asked to put in another checkbox that allows the user to select default reasons. For instance, we have 'Select All' that chooses all of the checkboxes and we have a 'Select Default' that only chooses a certain selection based on the product type...how can I indicate just a select few to be selected when that option is chosen?

                    Keep in mind that there are multiple different products so the options will be different for 'Select Default' based on the product.
                    As it seems you have already discovered, a good way to track this when the conditions are not as simple as those stipulated, is to use the Tag property of a control. Any value could be used for the tag simply to identify which are grouped together :
                    Code:
                    Private Sub SetAll(blnValue As Boolean) 
                      Dim ctl As Form.Control 
                      
                      For Each ctl In Me.Controls 
                        If ctl.Tag = "Group1" Then ctl = blnValue 
                      Next ctl 
                    End Sub

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      Originally posted by OldBirdman
                      No, it doesn't clear up anything. It seems like I assign .value to .value all the time. txtA = txtB, chkC = chkD, or txtE = cboF, etc. Never had any issues here with Access making the assignments.
                      Well, it seems Fish thinks I may be mistaken too. Possibly true. I'm a bit rushed at the moment to set up a test, but it's not too hard. Check it out.
                      Originally posted by OldBirdman
                      Because this code is in the form's module, can't the "me" be dropped completely?
                      Yes. It can. That doesn't mean it's a good idea though. Me. is more explicit, thus making the code clearer to a reader. Certainly not necessary though.
                      Originally posted by OldBirdman
                      As to having a "DeSelectAl l" checkbox, the code shown does not clear it when "SelectAll" is checked. Reflecting on this, I don't think there should be either a Select or DeSelect checkbox. They should be command buttons, "Select All" and "Clear All".
                      Good point, and good solution.

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        Originally posted by beacon
                        I think I found my answer on this website: ** Edit ** link removed.
                        Sorry about that. Our rules forbid linking to competing forums. We suffer enough from spam, and that would just make it too easy. I suspect your usage was innocently enough intended, but it had to be removed nevertheless.
                        Originally posted by beacon
                        I don't really understand how I was able to give a ctl a .Value when it wasn't available from the properties when I typed in the code, but it works. I know that you can't just type the following:
                        Code:
                        If ctl.Tag = "SelectAll" Then
                             ctl.Value = True
                        Else
                             ctl.Value = False
                        End If
                        ...because it will give you an error message similar to the runtime error 438 I was receiving when I initially began this process.

                        You have to put in some work to add the tags to each of the checkboxes, but once you get that entered it works pretty well.

                        Thanks for everybody's input...
                        If ctl is Dimmed as a simple Form.Control, then this makes sense. Intellisense.

                        Intellisense (prompting you with a list of available properties) can only prompt with what it knows about. If the variable had been designed as a Form.CheckBox, then it would have known it had a .Value property. As it is defined as a generic control then it doesn't know that.

                        Your code wouldn't work as written if your form had any controls without the .Value property (EG. Label).

                        PS. Please check out OB's post where he suggests using command buttons instead of checkboxes for this. He makes a good point.

                        Comment

                        • beacon
                          Contributor
                          • Aug 2007
                          • 579

                          #13
                          Hi NeoPa,

                          Sorry about the link...I questioned whether or not I should include. I'm really in the habit of including my sources for school and thought it best to give credit where it's due. Thanks for clearing that up...I won't include links to other forums in the future.

                          I read what OB said about the command buttons and I tried that, but I didn't have any luck initially...I still got the 438 error message.

                          Now that I've gotten the solution I listed...I guess it really doesn't matter if I use a checkbox or a command button. The checkbox will add or remove checks from the other checkboxes depending on whether or not it's value is true or not, so it basically serves the same purpose as a select/clear command button. Heck, a toggle button that changed it's caption from 'Select' to 'Clear' might even be neat/useful.

                          My task, now that the checkboxes work for default and select all conditions, is to find a way to link the default reason to the product that's chosen. I don't think I'll have any problems...just a lot of repetitive code to address all the possible conditions.

                          Thanks again, as always, for all your help...

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            Originally posted by beacon
                            Hi NeoPa,

                            Sorry about the link...I questioned whether or not I should include. I'm really in the habit of including my sources for school and thought it best to give credit where it's due. Thanks for clearing that up...I won't include links to other forums in the future.
                            Really not a problem. I guessed as much. Incidentally, links to sites hosted by the provider will always be acceptable (MSDN for instance, or an MS web page for Access).

                            Oh, and have fun with your tagging ;)

                            Comment

                            Working...