Command "Save" Override

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ezzz
    New Member
    • Jan 2010
    • 28

    Command "Save" Override

    I have dissabled the auto save function of my form, to prevent accidental editing by using:-
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("Changes have been made to the form. Are you sure you want to save the changes ?", vbYesNo) <> vbYes Then
        Cancel = True
        Me.Undo
        Exit Sub
        End If
    End Sub
    The problem now is that when i use my command "Save" button it also brings up the message box asking if i want to save or not. Is there a way to get my "Save" button to bypass this function and just save the changes?
    Regards
    Eric
    Last edited by Stewart Ross; Feb 18 '10, 07:41 PM. Reason: Please use the [Code][/Code] tags for the code in your question
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi, and Welcome to Bytes!

    You can't bypass the BeforeUpdate event as it is occurring correctly in response to the wanted record save. A way round it is to declare a public variable (a global variable for that form) at the top of the code module for the form, set it to a known value in your Save routine, then test for this value in the BeforeUpdate event:

    Code:
    Public blBypassMsgBox as Boolean
    .
    .
    Private Sub YourSave_Click()
       blBypassMsgBox = True
    .
    .
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If blBypassMsgBox then 
      blBypassMsgbox = false ' only needed if the form is likely to stay open after the save
      Exit Sub
    End If
    If MsgBox("Changes have been made to the form. Are you sure you want to save the changes ?", vbYesNo) <> vbYes Then
        Cancel = True
        Me.Undo
        Exit Sub
        End If
    End Sub
    -Stewart

    Comment

    • Ezzz
      New Member
      • Jan 2010
      • 28

      #3
      Stewart you are the man, form works a treat.
      Many thanks again
      Ezzz

      Comment

      Working...