A couple of problems:
Originally Posted by
Dal Jeanis
Code:
Private Sub cmdSave_OnClick()
Me.Dirty = False
End Sub
Although we refer to the OnClick event, the Sub Header would actually be
Private Sub cmdSave_Click()
That aside
Originally Posted by
Dal Jeanis
Code:
Private Sub Form_BeforeUpdate()
If Me.Dirty Then Me.Undo
End Sub
the Form_BeforeUpdate event will only execute if the Form is Dirty. If it's not Dirty, the event will not fire, and if it doesn't fire, the Record can never be Saved! Since the Form is Dirty, when the Form_BeforeUpdate executes, then
Code:
Private Sub Form_BeforeUpdate()
If Me.Dirty Then Me.Undo
End Sub
means, once again, that the Record could never be Saved! And because of this, Access will not allow code like this, and will pop an error.
While there are hacks out there to do what you want (I have one of John Vinson's archived that I can give you) why not simply let Access work as it is intended to work, and using the Form_BeforeUpdate event, ask the user if they want to Save the Record or dump it as they leave the Record or Close the Form?
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not (Me.NewRecord) Then
If MsgBox("Would You Like To Save The Changes To This Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save Changes to Record ???") = vbNo Then
Me.Undo
End If
Else
If MsgBox("Would You Like To Save This New Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save This New Record ???") = vbNo Then
Me.Undo
End If
End If
End Sub
You can still add a "Save" button, if some users expect it, merely having it set up to force a Save; using
DoCmd.RunCommand acCmdSaveRecord
Linq ;0)>
The problem with making anything foolproof...is that fools are so darn ingenious!
All posts/responses based on Access 2003/2007