Originally Posted by
June7
Don't think I have ever used form Form_BeforeUpdate event. I also use custom buttons for navigation and saving/deleting record. I disable form X close and navigation bar. I would have code in button click event to check if control has input and then proceed accordingly. Have a Sub or Function that can be called by multiple buttons.
I think I agree with you on not using the before update event. The only reason why I do is because I am trying to take care of the scenario when the user closes the app by right clicking the Access icon in the taskbar which closes everything and saves it automatically. So by doing some code for the before update event, this can handle prompts to ask the user before it closes out, if they want to save the changes to a record or not.
If there is a way to stop/cancel the before update event if the user actually uses all of the buttons in the form, that would solve this problem. I could do all the code behind every button to handle everything and only if the user closes the app from the taskbar, then have the before update event get triggered to ask if they want to save any changes or not. Is this possible?
If not, I been messing around did some more changes. Right not I am only using the previous record button for testing and I also started changing some of the code for the save button. The before update event has been changed as well:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.txtClerk <> Me.txtUpdatedBy Then
Beep
MsgBox "You Are Not Authorized To Edit This Record.", vbCritical, "Unauthorized Access"
MsgBox "Changes Will Not Be Saved.", vbCritical, "Notification"
DoCmd.RunCommand acCmdUndo
Exit Sub
End If
If IsNull(Me.cmbSubject) Then
MsgBox "Please Enter A Subject.", vbInformation, "Requirements"
Me.cmbSubject.SetFocus
Exit Sub
End If
If Me.NewRecord And Me.Dirty Then
If MsgBox("Save New Record?", vbYesNo, "Save Record") = vbYes Then
If IsNull(Me.txtTimeOfService) Then
Me.txtTimeOfService = Time()
End If
If IsNull(Me.txtDateOfService) Then
Me.txtDateOfService = Date
End If
DoCmd.GoToRecord , , acNewRec
Exit Sub
Else
Me.Undo
End If
End If
If Me.Dirty Then
If MsgBox("Save Changes To Record?", vbYesNo, "Save Record") = vbYes Then
If IsNull(Me.txtTimeOfService) Then
Me.txtTimeOfService = Time()
End If
If IsNull(Me.txtDateOfService) Then
Me.txtDateOfService = Date
End If
Exit Sub
Else
Me.Undo
End If
End If
End Sub
Private Sub cmdPrevious_Click()
If Me.NewRecord And Me.Dirty = False Then GoTo SkipToHere
If IsNull(Me.cmbSubject) Then
MsgBox "Please Enter A Subject.", vbInformation, "Requirements"
Me.cmbSubject.SetFocus
Exit Sub
End If
SkipToHere:
DoCmd.GoToRecord , , acPrevious
Me.txtCustNum.SetFocus
End Sub
Private Sub cmdSave_Click()
If Me.Dirty = False Then
MsgBox "No Changes Were Made.", vbInformation, "Notification"
Me.txtCustNum.SetFocus
Exit Sub
End If
If Me.NewRecord And Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToRecord , , acNewRec
Me.txtCustNum.SetFocus
End If
If Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
Me.txtCustNum.SetFocus
End If
End Sub
Will be back to report what issues I get with this setup. I did a little testing earlier and one of the scenarios wasn't working.