Cancel BeforeUpdate Event

wjoc1

Registered User.
Local time
Today, 10:26
Joined
Jul 25, 2002
Messages
117
Hi,

I have a form and if the user makes changes to a record I use the BeforeUpdate event of the form to ensure they wish to save these changes before moving to a new record.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If MsgBox("Data in the current record has been changed since the last save" & Chr(13) & _
"Do you wish to save these changes?", vbYesNo, "Rural Blocks") = 7 Then
        Me.Undo
    End If
    
End Sub

That works just fine. Now, I also have a button on the form which allows the user to explicitly save the record.
Code:
Private Sub cmdSave_Click()

    DoCmd.RunCommand acCmdSaveRecord

End Sub

However if I click the save button the prompt from the msgbox in the forms BeforeUpdate event is displayed also. Is there any way I can stop this i.e. cancel the BeforeUpdate for the form when the save button is used.

Thanks,
Liam
 
Liam,

Create a module scope flag...

Code:
Private DontPromptUser as Boolean

Option Explicit
---------------------------

Set and reset the flag in your save sub...

Code:
Private Sub cmdSave_Click()

    DontPromptUser = True	
      DoCmd.RunCommand acCmdSaveRecord
    DontPromptUser = False

End Sub

Check the flag in the BeforeUpdate sub...

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If DontPromptUser = True then exit sub

    If MsgBox("Data in the current record has been changed since the last save" & Chr(13) & _
"Do you wish to save these changes?", vbYesNo, "Rural Blocks") = 7 Then
        Me.Undo
    End If
    
End Sub

Regards,
Tim
 
wjoc1 said:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If MsgBox("Data in the current record has been changed since the last save" & Chr(13) & _
"Do you wish to save these changes?", vbYesNo, "Rural Blocks") = 7 Then
        Me.Undo
    End If
    
End Sub

Observation: Don't use Me.Undo - Replace this with Cancel = True
 
Last edited:
Code:
Private DontPromptUser as Boolean

Option Explicit
---------------------------

Option Explicit must be the first line of code in a module.
 
Mile-O-Phile said:
Code:
Private DontPromptUser as Boolean

Option Explicit
---------------------------

Option Explicit must be the first line of code in a module.

Not true. It just must be above all procedures. At least in my version of Access.

Regards,
Tim
 

Users who are viewing this thread

Back
Top Bottom