Crobar,
Insert the declaration at the top of your form, before any Subs or Functions.
The next block of code is added to the OnClick event of your Save Button (you must have a Save Button to use the code).
The third block of code is added to the Form's BeforeUpdate Event. Your code on the form should look something like this:
- Option Compare Database
-
Option Explicit
-
-
Private bSaveClicked as Boolean
-
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If Not bSaveClicked Then
-
MsgBox "You are trying to navigate away from the " & _
-
"active record. Please either save your " & _
-
"changes, or press ESC to cancel your changes.", _
-
VbOkOnly + vbInformation
-
Cancel=True
-
End If
-
End Sub
-
Private Sub cmdSave_Click()
-
bSaveClicked=True
-
On Error Resume Next
-
Docmd.Runcommand acCmdSaveRecord
-
bSaveClicked=False
-
End Sub
Thanks to Smiley for the code (I did change the name of the Class variable to match the rest of the code).
NB: I did not include any error handling, which I always recommend for every sub and function.
Hope this hepps!