Access automatically saves the record when the user does any of the
following:
- moves to another record
- applies a filter
- changes the sort order
- chooses Save Record from the Records menu
- presses Ctrl+F4, Alt+F4, Alt F+C, or Alt F+X, or Shift+Enter
- closes the form, or closes Access.
and so on.
There are also bunches of ways things that can trigger the saving
programmatically.
The only way to catch them all is to use the event Access exposes to you
before the record is saved: the BeforeUpdate event of the form. Cancel this
event if you do not want the record to save.
Of course, the best solution would be to move your code into the
Form_BeforeUpdate event. It will then be run whenever the record is about to
be written to the table. You can validate anything you wish, cancel the
event, and undo the form if you are not happy with the results. And you have
not tied the user into that horrid straightjacket of "save only by clicking
my button" which just screams that the developer did not understand how to
use the Access events.
This example ensures the EndDate is not before the StartDate, and warns the
user if they left the Surname field blank:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Me.EndDate < Me.StartDate) Then
Cancel = True
MsgBox "Cannot end before it begins
Else
If IsNull(Me.Surname) Then
If MsgBox("Save without surname?", vbYesNo) = vbNo Then
Cancel = True
'Me.Undo
End If
End If
End If
End Sub