FWIW here's the code from the module a form with Save and Undo buttons, in
which I've added a line to move to a new record after a save, and commented
out the lines which in my original, where focus remains on the current record,
disable the buttons. These are unnecessary as this is covered by the Current
event procedure's code:
' updates can only be saved via command button
Option Compare Database
Option Explicit
Dim blnSaved As Boolean
Private Sub cmdSave_Click()
Const MESSAGETEXT = "Save record?"
If Me.Dirty Then
' if user confirms set variable to True and attempt to save record
If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbYes Then
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
' if record cannot be saved set variable to False
If Err <> 0 Then
blnSaved = False
Else
' disable buttons
Me.AddressID.SetFocus
'Me.cmdSave.Enabled = False
'Me.cmdUndo.Enabled = False
' Move to new record
DoCmd.GoToRecord acForm, Me.Name, acNewRec
End If
Else
blnSaved = False
End If
End If
End Sub
Private Sub cmdUndo_Click()
' undo edits
Me.Undo
' disable buttons
Me.AddressID.SetFocus
Me.cmdSave.Enabled = False
Me.cmdUndo.Enabled = False
End Sub
Private Sub Form_AfterUpdate()
' reset variable to False
blnSaved = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
' cancel update if variable is False,
' i.e. save button has not been clicked
If Not blnSaved Then
Cancel = True
End If
End Sub
Private Sub Form_Current()
' reset variable to False
blnSaved = False
' disable buttons
Me.cmdSave.Enabled = False
Me.cmdUndo.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
' enable buttons
Me.cmdSave.Enabled = True
Me.cmdUndo.Enabled = True
End Sub
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const IS_DIRTY = 2169
' suppress system error message if form
' is closed while record is unsaved,
' NB: changes to current record will be lost
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If
End Sub
Ken Sheridan
Stafford, England
David G. wrote:
>I am having issues enabling and disabling Save and Cancel buttons on a
>form in continuous form view. I'm using the Dirty and Current events
>to enable and disable as the user tabs or clicks through records.
>(Works fine.)
>
>For cancel, I move the focus to any of the form fields, then disable
>Cancel. With Save, if I move the focus to a form field, it selects the
>active record row instead of moving onto a new record.
>
>Does any one know of an example I might see how this scenario is best
>handled?
>THANKS!
>THANKS!
>David G.
>> Stay informed about: Implementing Save and Cancel command buttons on a form