Detecting Change

wchernock

Registered User.
Local time
Yesterday, 19:18
Joined
Jun 18, 2007
Messages
28
Does someone have an example of code that detects if data has changed on a form?

Something like:
If change detected then undo changes
else do not save the record

I have an initial form that lists data (view only). I then have Add, Update, and Delete command buttons that open individual forms. On the next form, I want to have code that cancel the changes (if any have occurred). I used the UNDO option but if no changes have occured, it gives an error.
 
Just use the Before Update event of the form. If no changes have been made it won't fire.
 
If Me.Dirty Then
Me.Undo
Else
MsgBox "There is nothing to undo"
EndIf
 
If Me.Dirty Then
Me.Undo
Else
MsgBox "There is nothing to undo"
EndIf

Rich - wouldn't this be even shorter code and work?

In the Before Update event use:
Code:
Cancel = True
Me.Undo

No test for dirty necessary because the Before Update event won't fire if nothing has changed.
 
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

or

Docmd.RunCommand acCmdUndo
 
So my code would be something like

If me.dirty then
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Else
DoCmd.Close acForm, CurrentForm, acSaveNo
 
Works like a charm

Added the code

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
DoCmd.Close acForm, CurrentForm, acSaveNo
Else
DoCmd.Close acForm, CurrentForm, acSaveNo
End If
 
Glad to hear it. Menucommand still works in current versions of Access, but is considered obsolete as Rich said, which is why I the Runcommand. Either should do just fine for what you are trying to accomplish.
 
I am closing a custom add form and opening the original list. I chose not to use a popup form with modal setting overlaying the original form.

Is there something else I should be considering?
 

Users who are viewing this thread

Back
Top Bottom