what is the save command code in ms access?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emmannuelle15
    New Member
    • Jun 2018
    • 5

    what is the save command code in ms access?

    hello guys im a beginner in programming .. i have a problem in saving option in access... in access when you type information in fields it automatically save in database if you click the next button command even if you are not clicking the save button..
    and i want is, the data will not be save if the save button command is not using..

    (sorry for the grammar english is not my first language)

    hope you understand my point .. thank you :)
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32668

    #2
    Hi Emmannuelle.

    There are two parts to this question :
    1. What code is used to save a record?
    2. How do I ensure it only saves when I use the save code, and not simply when the operator moves to another record?


    For #1 I have a routine as the code itself is such that it doesn't look like it's going to save the record.
    Code:
    'SaveRec() Saves the current record on frmMe.
    Public Sub SaveRec(frmMe As Form)
        'No error handling here.  Should be handled by calling code if required.
        'This code is a little weird but how saving has been implemented in Access.
        frmMe.Dirty = False
    End Sub
    This is part of a standard module that can be called by the code from any Form.

    For #2 we use a variable that is only set to allow saves from the part of our code where we want the saves to succeed. The variable is set as Private in the Form's code module. In the BeforeUpdate() Event handler code you use the Cancel parameter if that variable isn't set, so the save fails.
    Code:
    Private blnAllowSave As Boolean
    ...
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not blnAllowSave Then
            Cancel = True
            Call MsgBox(Prompt:="Please use the Save Button to save your changes." _
                      , Buttons:=VbOKOnly Or VbInformation _
                      , Title:=Me.Name)
        End If
    End Sub
    ...
    Private Sub cmdSave_Click()
        blnAllowSave = True
        Call SaveRec(frmMe:=Me)
        blnAllowSave = False
    End Sub

    Comment

    • emmannuelle15
      New Member
      • Jun 2018
      • 5

      #3
      thank you sir NeoPa i will try it sir.. ill post here if it works
      thanks a lot :D

      Comment

      • emmannuelle15
        New Member
        • Jun 2018
        • 5

        #4
        sir NeoPa your 2nd code really did work .. but im having a problem in the save code...


        Code:
        Private Sub cmdsave_Click()
          Public Sub SaveRec (frmMe As Form)
        frmMe.Dirty = False
        
        End Sub
        End Sub
        thats where the error is ... i think i write it wrong in the code

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3665

          #5
          emmannuelle15,

          First, you should do a direct copy and paste of the code provided by NeoPa. It is clear from your last post that this did not happen.

          When you think of a procedure, think of it as a distinct unit of code. Anything between the Private Sub [NameOfProcedure]() and End Sub statements are autonomous and can't have anything inside except what belongs specifically to that procedure.

          For your preivous post remove lines 1 and 6 to start. You must also have the second portion of NeoPa's code in your form's VBA module. It should work "as is", using your command button.

          Hope this hepps.
          Last edited by NeoPa; Jun 14 '18, 12:11 PM. Reason: Typo of Priavte.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32668

            #6
            Indeed. Use Copy/Paste (Recommended) or, if that's not possible because your names are different, at least Copy/Paste first, and then change only those names you need to.

            If you can simply copy visually then fine, but you need to be able to do it 100% reliably if you do. Why bother though, when using Copy/Paste is both easier and more reliable?

            Comment

            • emmannuelle15
              New Member
              • Jun 2018
              • 5

              #7
              sir twinnyfo and sir NeoPa thank you for the advice i really appreciate it.. ill keep it in mind :D

              Comment

              • twinnyfo
                Recognized Expert Moderator Specialist
                • Nov 2011
                • 3665

                #8
                Were we able to resolve your primary issue?

                Comment

                • emmannuelle15
                  New Member
                  • Jun 2018
                  • 5

                  #9
                  yes sir :D thank you

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32668

                    #10
                    Good to see. This is a helpful thread as many need help with exactly that.

                    Comment

                    Working...