How do I Save Data on Form Only After Button Clicked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barzan
    New Member
    • Jan 2012
    • 1

    How do I Save Data on Form Only After Button Clicked

    Hi There

    I have a data entry form bound to table , i want to add records only using command button in other words i want to prevent automatic saving while writing into fields .

    Thanks
  • dsatino
    Contributor
    • May 2010
    • 393

    #2
    Then you don't want to bind the bind the form to the table. Just capture the values entered in a sql string and run that on the button click.

    Comment

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

      #3
      I disagree dsatino. There is absolutely no need to resort to unbound forms and the headeaches that come with that.


      Add a private boolean variable to your form:
      Code:
      Private bSaveClicked as Boolean
      Add code to your command button:
      Code:
      bSaveClicked=true
      On Error Resume Next
      Docmd.Runcommand accmdsaverecord
      bSaveClicked=false
      Add code to your forms beforeupdate:
      Code:
      If not bSaveClicked Then
        Msgbox "You are trying to navigate away from the active record. Please either save your changes, or press ESC to cancel your changes.",VbOkOnly+vbInformation
      Cancel=True
      End If
      Last edited by TheSmileyCoder; Jun 13 '17, 09:50 AM.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        I must agree with Smiley here (Sorry Dsatino). The SQL string approach should be avoided where possible, as it is trying to do a job for Access (badly) that Access already does well. Many people seem to see this as a way forward, but I have never seen anything but trouble come from leading people away from the natural way of using Access. It's a bit like using an Object-Oriented computer language, but writing procedural code anyway.

        May I just add that this reflects no disrespect towards Dsatino, whom I know to be an accomplished Access expert in various ares. In this case though, we disagree.

        Comment

        • dsatino
          Contributor
          • May 2010
          • 393

          #5
          No disrespect taken. A better way is a better way... :)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            A new member, Crobar, posted a request for help with Smiley's code in here so I moved it to a new thread (How Do I Use Code Posted?).

            Comment

            • ocean11
              New Member
              • Jun 2017
              • 1

              #7
              Originally posted by TheSmileyCoder
              I disagree dsatino. There is absolutely no need to resort to unbound forms and the headeaches that come with that.


              Add a private boolean variable to your form:
              Code:
              Private bSaveButtonClicked as Boolean
              Add code to your command button:
              Code:
              bSaveClicked=true
              On Error Resume Next
              Docmd.Runcommand accmdsaverecord
              bSaveClicked=false
              Add code to your forms beforeupdate:
              Code:
              If not bSaveClicked Then
                Msgbox "You are trying to navigate away from the active record. Please either save your changes, or press ESC to cancel your changes.",VbOkOnly+vbInformation
              Cancel=True
              End If
              Replace "bSaveButtonCli cked" with "bSaveClick ed"

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Good spot :-)

                Comment

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

                  #9
                  Thank you. I've fixed the typo. I guess this falls under the category "better late than never"

                  Comment

                  Working...