Results 1 to 3 of 3
  1. #1
    abodi is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2011
    Posts
    31

    Question error handling/ if-then and calling a separate Sub

    I've got a form. i've got 2 buttons that work perfectly individually, but i want to combine them.
    I also only want to save the record if the email is sent. and not if they cancel the email or another error.

    This is the code i've got. I hope you can point out the glaringly obvious error i am missing.

    --------------------
    Option Compare Database
    Private Sub cmdSave_NLR_Click()
    Call SendEmail
    If Err.Number = 0 Then
    <what am i missing here. I previously had call Sub Save_NLR but it was executing regardless of the email outcome>
    End If
    End Sub
    Private Sub SendEmail()
    On Error GoTo HandleErr

    '-- 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.
    Exit Sub

    HandleErr:
    If Err.Number = 2501 Then
    MsgBox "The automated email was cancelled or failed." & Chr$(13) & "Please inform the applicant of the status of this application"
    Else
    MsgBox Err.Description
    End If

    End Sub

    '-- Saves the record
    Private Sub Save_NLR()
    On Error GoTo cmdSave_NLR_Click_Err
    On Error Resume Next
    DoCmd.RunCommand acCmdSaveRecord
    If (MacroError <> 0) Then
    Beep
    MsgBox MacroError.Description, vbOKOnly, ""
    End If

    cmdSave_NLR_Click_Exit:
    Exit Sub
    cmdSave_NLR_Click_Err:
    MsgBox Error$
    Resume cmdSave_NLR_Click_Exit
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    I would probably have the mail sub be a function that returned true or false as appropriate. Instead of testing the error, test the function result.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    hapm is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    197
    Or catch the error in your click event sub instead of catching it in your SendMail sub. Its best practice to catch errors at the places where you know what to do when they occur. In SendMail you don't know what to do with it: SendMail could be called by anything, like an automated process, user interaction or something else. You don't know it. In your click event sub, you know that the call comes from a user, who pressed a button. So if an error occurs when the user requests an action to be done, its best to inform him with a message box, that the operation failed. So you know what to do when you get an error there. It would be something else if the SendMail was called from an automated process. In this case the error would probably be logged to a log table or something else would happen. So catch errors in places where you know what to do with them, and not at places where they occur, and you don't know what to do.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calling Error
    By drunkenneo in forum Programming
    Replies: 6
    Last Post: 09-12-2013, 01:32 PM
  2. error handling
    By slimjen in forum Forms
    Replies: 6
    Last Post: 03-13-2013, 11:49 AM
  3. Error Handling Question - On Error Goto
    By redbull in forum Programming
    Replies: 7
    Last Post: 12-06-2012, 07:54 AM
  4. Replies: 3
    Last Post: 09-05-2012, 10:23 AM
  5. Error 2501 displays with Error handling
    By agent- in forum Programming
    Replies: 13
    Last Post: 08-05-2011, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums