BeforeUpdate with a "new record" button

347 Views Asked by At

I have an Access form users sometimes forget to save. I put in a BeforeUpdate trigger to pop up message reminding users to save or cancel before taking any action.
I found this code on the net and it works for everything except my "New Record" button.
As far as I know Me.Dirty should do the trick.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim ctl As Control
    On Error GoTo Err_BeforeUpdate

    ' The Dirty property is True if the record has been changed.
    If Me.Dirty Then
        ' Prompt to confirm the save operation.
        If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
          "Save Record") = vbNo Then
            Me.Undo
        End If
    End If

Exit_BeforeUpdate:
    Exit Sub

    Err_BeforeUpdate:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_BeforeUpdate
End Sub

The code for the new record

Private Sub new_Click()
    njno = NewJobNbr()
    Job.Value = njno
    RnK.Value = ""
    Date_Requested.Value = ""
    Est_Time.Value = ""
    Originator.Value = ""
    Date_Required.Value = ""
    Description.Value = ""
    Reason_for_Request.Value = ""
    Comments.Value = ""
    Priority_Tasks.Value = ""
    TName.Value = ""
    Required.Value = ""
    Costing.Value = ""
    Completed.Value = ""
    Date_Completed.Value = ""
End Sub
1

There are 1 best solutions below

0
Fil On

I don't really understand what njno is. If the textboxes are bound to the form's RecordSource then. On form Current event use:

Private Sub Form_Current ()

If Me.NewRecord Then
Job.Value = njno 
End if

End Sub

Then on the button Click event use:

Private Sub new_Click()
DoCmd.GoToRecod,,acNewRec
End sub