Solved Multiple error handling on one button (1 Viewer)

mistyinca1970

Member
Local time
Yesterday, 22:33
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
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:33
Joined
Oct 29, 2018
Messages
21,467
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
 

bastanu

AWF VIP
Local time
Yesterday, 22:33
Joined
Apr 13, 2010
Messages
1,402
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,
 

mistyinca1970

Member
Local time
Yesterday, 22:33
Joined
Mar 17, 2021
Messages
117
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!
 

bastanu

AWF VIP
Local time
Yesterday, 22:33
Joined
Apr 13, 2010
Messages
1,402
It does but it lumps in together all errors other than 2501 which could be confusing for the users.
Cheers,
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:33
Joined
Oct 29, 2018
Messages
21,467
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

Top Bottom