Solved Multiple error handling on one button

mistyinca1970

Member
Local time
Yesterday, 17:23
Joined
Mar 17, 2021
Messages
117
I have the following error handling added to a button, but I want to add another handling for another type of error. This one in particular is if the user fails to select a report from the list box prior to clicking the "Email Report" button.
Code:
Private Sub btnEmailReport_Click()
On Error GoTo btnEmailReport_Click_Err

    DoCmd.SendObject acReport, lstReportList, "PDFFormat(*.pdf)", TempVars!EmailAddress, "", "", "Contrax™ " & lstReportList, "This is an AUTOMATIC NOTIFICATION sent from Contrax™. Please see the attached Report. You may direct any questions regarding this report to Mickey Mouse.", True, ""


btnEmailReport_Click_Exit:
    Exit Sub

btnEmailReport_Click_Err:
   MsgBox "You must first select a report from the List Box.", vbOKOnly, "No Report Selected"
    Resume btnEmailReport_Click_Exit

End Sub

But I also want to add a custom message for if/when the user closes the email and doesn't send it. I have this code, but I don't know how to insert it with the other error handling.

Code:
Cleanup:
    Exit Sub

ErrorHandler:
Select Case Err.Number
    Case 2501
      MsgBox "Your email message has been canceled.", vbOKOnly, "Email Canceled"
    Case Else
      MsgBox Err.Number & ": " & Err.Description
  End Select
  Resume Cleanup
 
Hi. You could try:
Code:
btnEmailReport_Click_Err:
Select Case Err.Number
    Case 2501
      MsgBox "Your email message has been canceled.", vbOKOnly, "Email Canceled"
    Case Else
      MsgBox "You must first select a report from the List Box.", vbOKOnly, "No Report Selected"
End Select

  Resume btnEmailReport_Click_Exit
 
Your second piece has your answer, use Select Case Err.Number to customize your messages. Write down what error number corresponds to your first error (no report) and add a case for that.

Cheers,
 
Hi. You could try:
Code:
btnEmailReport_Click_Err:
Select Case Err.Number
    Case 2501
      MsgBox "Your email message has been canceled.", vbOKOnly, "Email Canceled"
    Case Else
      MsgBox "You must first select a report from the List Box.", vbOKOnly, "No Report Selected"
End Select

  Resume btnEmailReport_Click_Exit
Thank you. This works!
 
It does but it lumps in together all errors other than 2501 which could be confusing for the users.
Cheers,
 
Thank you. This works!
You're welcome. But, as Vlad said, you might want to find out the actual error number when the user does not select a report from the list, so you can add it to the code for clarity. Cheers!
 

Users who are viewing this thread

Back
Top Bottom