Hi Everyone,
I have a form with fields that people enter information into.
I have code to confirm all fields are not empty - This works.
The code then sets up an email with details from the email - Generating the email is fine.
Then this is where i'm clearly missing something about error handling.
If the email sends (no error)
Then I would like the record to save, clear the form (i've not done this yet) and move to the next record (currently it keeps saving over the current record.)
Else the email doesn't send and it does generate the error, but at this point i'd like the proceedure to exit without clearing the form or saving the record.
Essentially unless the email sends no action is taken.
Currently the silly thing, saves regardless and keeps saving over the current record.
Here is my feeble code.
Any help on where i'm going wrong would be greatly appreciated.
Code:
Private Sub cmdSave_NLR_Click()
On Error GoTo HandleErr
TestingFields:
If NLR_ApplicantName = "" Or CboTeamNumber = "Please Select" Or LeaveType = "Please Select" Or LeaveStartDate = "" Or LeaveEndDate = "" Or Status = "Please Select" Or ApprovedBy = "" Then
MsgBox "You must complete all the required fields."
Exit Sub
End If
EmailProc:
'-- The Variables are used for sending the leave request email.
Dim stRecipient As String '-- This variable will contain the primary recipient.
Dim stCC As String '-- This variable is who is included CC.
Dim stSubject As String '-- This variable is the subject line.
Dim stBody As String '-- This variable is the body of the Email.
'-- This sets the information contained in the variables.
stSubject = "UMG Leave Request is " & Status & ". ** " & stGivenName & " " & stSurname & " from Team " & CboTeamNumber
stRecipient = NLR_ApplicantName
stBody = "Your leave request for " & LeaveStartDate & " to " & LeaveEndDate & " has been " & Status & "." & Chr$(13) & Chr$(13) & _
"What you need to do next...."
DoCmd.SendObject , , acFormatTXT, stRecipient, "Jimmy@abc.com", , stSubject, stBody, -1 '-- Email command.
HandleErr:
If Err.Number = 2501 Then
MsgBox "The automated email was cancelled or failed." & Chr$(13) & Chr$(13) & "Please inform the applicant of the status of this application"
Exit Sub
Else
MsgBox Err.Description
End If
End Sub
Private Sub Save_NLR() '-- Saves the record
On Error GoTo cmdSave_NLR_Click_Err On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToRecord , "", acNext
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
cmdSave_NLR_Click_Exit:
Exit SubcmdSave_NLR_Click_Err:
MsgBox Error$
Resume cmdSave_NLR_Click_ExitEnd Sub
Looking at the code, i'm not sure how the second sub is even being called