Hi Emmannuelle.
There are two parts to this question :
- What code is used to save a record?
- How do I ensure it only saves when I use the save code, and not simply when the operator moves to another record?
For #1 I have a routine as the code itself is such that it doesn't look like it's going to save the record.
- 'SaveRec() Saves the current record on frmMe.
-
Public Sub SaveRec(frmMe As Form)
-
'No error handling here. Should be handled by calling code if required.
-
'This code is a little weird but how saving has been implemented in Access.
-
frmMe.Dirty = False
-
End Sub
This is part of a standard module that can be called by the code from any Form.
For #2 we use a variable that is only set to allow saves from the part of our code where we want the saves to succeed. The variable is set as
Private
in the Form's code module. In the
BeforeUpdate()
Event handler code you use the
Cancel
parameter if that variable isn't set, so the save fails.
- Private blnAllowSave As Boolean
-
...
-
Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If Not blnAllowSave Then
-
Cancel = True
-
Call MsgBox(Prompt:="Please use the Save Button to save your changes." _
-
, Buttons:=VbOKOnly Or VbInformation _
-
, Title:=Me.Name)
-
End If
-
End Sub
-
...
-
Private Sub cmdSave_Click()
-
blnAllowSave = True
-
Call SaveRec(frmMe:=Me)
-
blnAllowSave = False
-
End Sub