Remove validation from save process

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Belimisimus
    New Member
    • Mar 2010
    • 18

    Remove validation from save process

    Hello, everyone!

    I add a "Yes" "No" "Cancel" option on the before update event of the form.
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
                answer = MsgBox("Do you want to save changes?", vbYesNoCancel + vbQuestion, "Confirm Save")
                If answer = vbNo Then
                    Me.Undo
                ElseIf answer = vbCancel Then
                    Cancel = True
                End If
    End Sub
    Also have save button on top of the form.
    Code:
    Private Sub Save_Click()
        On Error Resume Next
        DoCmd.RunCommand acCmdSaveRecord
    End Sub
    Is there a way to skip msgbox from BeforeUpdate in case when I click Save button? Obviously, if I click save I want to save... there is no need to ask me validation.
    Last edited by NeoPa; Mar 16 '10, 05:46 PM. Reason: Please use the [CODE] tags provided.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Hi and Welcome to Bytes.

    Please remember to use the [CODE][/CODE] tags provided around your code.

    What I do, is to have a private variable bSaving declared in the forms module, at the top, outside any procedure.

    The Form_BeforeUpda te should then look:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
      If Not bSaving Then
        answer = MsgBox("Do you want to save changes?", _
                      vbYesNoCancel + vbQuestion, "Confirm Save")
        If answer = vbNo Then
          Me.Undo
        ElseIf answer = vbCancel Then
          Cancel = True
        End If
      End If
    End Sub
    With the save button:
    Code:
    Private Sub Save_Click()
      bSaving=True
      On Error Resume Next
      DoCmd.RunCommand acCmdSaveRecord
      bSaving=False
    End Sub

    Comment

    • Belimisimus
      New Member
      • Mar 2010
      • 18

      #3
      Nice and clear... the problem is solved!
      I'm newbie with VBA but this was very simple even for me!

      Instead of private, I add public variable, because I have multiple forms with "save", "save and new" and "close" button and everything is working just fine!

      Thx TheSmileyOne :)

      Comment

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

        #4
        Thats good to hear :)

        Comment

        Working...