Cancel saving a record

access7

Registered User.
Local time
Today, 18:53
Joined
Mar 15, 2011
Messages
172
Hello

I wonder if someone may be able to give me some advice on the following...

I am trying to get it so that if the user clicks cancel - and chooses that they DO NOT want to save the changes... that the changes are not saved (sounds simple enough... or so I thought). I have tried a few different things, mainly the me.undo and Cancel = True... I am not sure where I am going wrong, whether I have the wrong code, the wrong event or whether its due to a lot of my fields being on sub forms but unfortunately I cannot get the cancel button working properly. It would appear that when I change the data in a field and press cancel, the changes are still saved. Arghhh!
Below are some examples of code I have used, and I have also attached my db for a fuller picture... please help :o

Option Compare Database
Public CancelPressed As Boolean
Option Explicit
Private Sub CmdCancel_Click()

MsgBox "Are you sure you want to cancel any changes?", vbQuestion + vbYesNo, "Cancel?"
If vbYes Then
CancelPressed = True
Else
DoCmd.CancelEvent
End If

ControlEnabler False
ControlLocker True

Me.CmdSave.SetFocus

Me.CmdCancel.Visible = False
Me.CmdEdit.Visible = True

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Me.ModifiedBy = SUser
Me.ModifiedWhen = Now()

If CancelPressed = True Then
Cancel = True (note, I have also tried me.undo here)
End If

End Sub
 

Attachments

You are not resetting your CancelPressed variable to False. Why don't you use a checkbox instead or a Radio button which will give more visual to the user.
 

Users who are viewing this thread

Back
Top Bottom