A new record will only be initiated when the form moves to a new record if
it's Dirty property becomes True by a value being assigned to a field, either
manually or in code. The record will then be saved when the form moves to
another record, is closed, or the record is explicitly saved.
If you want certain fields to have a default value when the form goes to a
new record set the DefaultValue property of the control bound to the field
rather than assigning values to the controls. This does not dirty the form
so no new record is initiated. Note that if you are assigning a DefaultValue
in code this property is always a string expression, regardless of the data
type of the underlying field, so should always be wrapped in quotes. Often
this is not crucial, but sometimes it is (dates are a case in question), so
its prudent to do so regardless, e.g. to set a currency field to 1000:
Me.UnitPrice.DefaultValue = """" & 1000 & """"
If you want a user to be forced to explicitly save a record *after* entering
data then the following code in the form's module will do it; cmdSave is a
button to save the record:
''''module starts''''
' updates can only be saved via command button
Option Compare Database
Option Explicit
Dim blnSaved As Boolean
Private Sub cmdSave_Click()
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
If Err <> 0 Then
blnSaved = False
End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not blnSaved Then
Cancel = True
End If
End Sub
Private Sub Form_Current()
blnSaved = False
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const IS_DIRTY = 2169
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If
End Sub
''''module ends''''
Essentially it works by checking the value of a module level Boolean
variable before saving the record. The variable's value is by default
False, but is set to True when the button is clicked. If the value is False
then the return value of the form's BeforeUpdate event procedure's Cancel
argument is set to True, thus preventing the record from being saved by any
means other than clicking the button.
Ken Sheridan
Stafford, England