How to make a bound checkbox remain checked for new records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Monica
    New Member
    • Mar 2011
    • 5

    How to make a bound checkbox remain checked for new records

    I'm trying to do something I thought would be simple enough even for me, but I'm stuck. I have a checkbox on a form bound to a yes/no column in a table.

    I want the user to be able to check the box and have it remained checked as they enter new records, thus saving the tables field as YES for as many records as they wish until they uncheck it. Then when they uncheck it, have it remain unchecked as they create new records, etc...

    Eventually I'm going to create either a toggle or command button that does the checking of teh check box for them and probably hide the Check Box control, but that part I can figure out. It's getting it to stay checked or unchecked for each new record until toggled back that's throwing me.
    I feel really dumb even asking this question but I'm stumped. I know that an unbound checkbox kind of does what I want, but then how to get it to store the Yes/No answers where I want it to I don't know how to do either. Uhgg I'm so confused. Thanks in advance for your help.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Hi and welcome to Bytes

    What you want to do is "simply" modify the default value of your checkbox. Add code to the checkbox' Click Event as such:
    Code:
    Private Sub chkBox_Click()
        Me.chkBox.DefaultValue = Me.chkBox
    End Sub

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      In the after update event of the checkbox, you'll want to change the default of the checkbox to what it was just changed to.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        This is a bit complex to follow, so I'll lay it out as clearly as I can. First let me say that the other two answers are clearly correct.

        You need to start off with two checkboxes on your form. One for the bound field of your table. The other to indicate, and allow the operator to change, the value that should be treated as the default going forwards. Let's call them chkBound and chkUnbound for now.

        You explanation isn't entirely clear, so I'll assume you want to start a session with the default set to True. If this is not the case then please disabuse me. This value (True) should be set as the .DefaultValue in the design of the form for both controls.

        Whenever the operator changes the value in chkUnbound, the .DefaultValue of chkBound should be changed to reflect the new value (of chkUnbound).

        Comment

        • Jim Monica
          New Member
          • Mar 2011
          • 5

          #5
          Thank You everyone for your replies. I went with the first method as that seemed to be the simplest and with my limited brain power and knowledge, simple is good. Now my next task is to make a toggle button (I think) that allows the user to toggle the checkbox on/off or checked and unchecked. I know I could just use the checkbox for that, but my boss is insisting it be a button because it's also going to change the background of the form or give some other indicator to let the user know they are in the mode where the check box is checked. The ChkBox is there so that when the user is entering inventory items, the items are flagged as consignment items (if checked) and they will usually enter these items in a row in groups of about 50-100 which is why I needed the checkbox to stay checked until they uncheck it. Now I need to somehow Clearly tell the user they are in "Consignmen t Mode" thus telling them that all of the items they are entering are being flagged as consignment until they hit the button again and go back to "normal" mode. So that's what you guys (or gals) just helped me with and I REALLY appreciate it. Now to google Toggle button. Thank You again everyone!

          Comment

          • TheSmileyCoder
            Recognized Expert Moderator Top Contributor
            • Dec 2009
            • 2322

            #6
            I have tried to keep this simple:
            Basicly I have added a togglebutton togBtn and when its clicked it changes the caption on the button, and colours the detail area of the form. (Just for example, you could do other things).

            The only "complicate d" part is that I have added to the forms Current Event to only colour those records that are new (I.e. dont show the red consignment mode for existing records)

            Here is the code: (If this is a continues form you are using, then note that colouring the detail area is not the best approach to use, since it will colour all the records detail area, but you could colour the header area instead.
            Code:
            Private Sub Form_Current()
                If Me.NewRecord And Me.chBox.DefaultValue Then
                    Me.Detail.BackColor = vbRed
                Else
                    Me.Detail.BackColor = vbCyan
                End If
            End Sub
            
            Private Sub togBtn_Click()
                'Change default value
                    Me.chBox.DefaultValue = Not Me.chBox.DefaultValue
                    
                'Change caption on toggle button
                    If Me.chBox.DefaultValue Then
                        Me.togBtn.Caption = "Consignment Mode"
                        Else
                        Me.togBtn.Caption = "Other Mode"
                    End If
                
                'If this is a newrecord and default is true, color it
                    If Me.NewRecord And Me.chBox.DefaultValue Then
                        Me.Detail.BackColor = vbRed
                    Else
                        Me.Detail.BackColor = vbCyan
                    End If
            End Sub

            Comment

            • Jim Monica
              New Member
              • Mar 2011
              • 5

              #7
              Thanks very much for your fast reply. I feel kind of guilty because your basically writing this program for me with these tips. I really do appreicate your help. You've saved me so much time! Thanks again!

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Your boss clearly has experience with interfaces and how important that is when designing software.

                Smiley's solution includes everything I brought up, except what you put on the form is down to you of course, including the slight change to the spec from the original question (Different events required for CommandButton than CheckBox controls). He's even warned you about the continuous forms issue. As they say "You're good to go!"

                Comment

                • TheSmileyCoder
                  Recognized Expert Moderator Top Contributor
                  • Dec 2009
                  • 2322

                  #9
                  Your welcome.

                  Don't spend time on being guilty, spend the time on understanding WHY/HOW the code works. Too many people just copy code from this site(or others), without spending a single minute on understanding WHY/HOW it works.

                  Comment

                  Working...