Results 1 to 8 of 8
  1. #1
    DaveVlad is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2016
    Posts
    6

    Error trapping not working using VBA - runtime 2501

    I understand the need to error trap and give MS Access an option in the event of an error, but I am struggling with some code to email a report. It works fine provided the user doesn't cancel. If the user cancels, despite the code below, I get a runtime error 2501. Here is the code:

    on Error GoTo EmailErr



    strReptName = "HoYRandSSingleR"


    DoCmd.SendObject acSendReport, strReptName, acFormatPDF




    EmailExit:
    Exit Sub
    EmailErr:
    Select Case Err.Number
    Case 2501
    MsgBox "User cancelled the email."
    Case Else
    MsgBox Err.Number & ": " & Err.Description
    End Select
    Resume EmailExit

    Any help or advice gratefully received. I am using Access 2007 and Outlook in hybrid mode with Office 365 (I wonder if the problem might lie there).

    Regards

    Dave

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Error handling does not have to be done by Access, it can be manually done by you. Do "On Error Resume Next" and then after SendObject check for errors - e.g.
    If Err<>0 then
    If Err<> 2501 then
    ... your code here ...

  3. #3
    DaveVlad is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2016
    Posts
    6
    Thanks for the suggestion, Aytee

    Unfortunately, the problem still persists. I changed my code to:

    On Error Resume Next
    DoCmd.SendObject acSendReport, strReptName, acFormatPDF


    If Err = 2501 Then
    MsgBox "User cancelled the email."
    GoTo EmailExit
    End If

    But it still throws up a runtime error 2501.

    Regards

    Dave

  4. #4
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Maybe I misunderstood, I thought you wanted to bypass error checking if the user cancels. What exactly do you want to happen? And your code above does not handle all other errors which it must do (what about all other errors?).

  5. #5
    DaveVlad is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2016
    Posts
    6
    Hi Aytee

    I want to be able to cancel sending a report via email without it creating a runtime error. The report is generated dynamically using SQL (and that side of things works just fine - I can send the email too) but if the user cancels the send, it generates the runtime error 2501. If I can bypass the error, that would be great. However, the error occurs after the docmd.sendobject. I even tried inserting on error resume next after as well as before, but the runtime error still keeps cropping up. The EmailExit routine should handle any other errors.

    Regards

    Dave

  6. #6
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The user cancelled therefore you WILL get an error - every time. What you have to do with error handling is to ask yourself - what errors do I want to ignore and which ones are valid errors that tell me there is a problem?

    1 - error is 0: everything ok
    2 - error is 2501: user cancelled, that is fine so ignore
    3 - all other errors: trap them

    The EmailExit routine should handle any other errors.
    You may want to see what it contains - from here it looks like "Exit Sub", no error handling.

    Code:
    On Error Resume Next
    DoCmd.SendObject acSendReport, "Reportname", acFormatPDF
    If Err = 0 Then
    ElseIf Err <> 2501 Then
        MsgBox Err.Number & " " & Err.Description
    End If

  7. #7
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Might you have disabled error handling through the options in the VBA editor? (Tools - Options - General, then Error Trapping set to "Break on All errors?)? If you did, then all On Error Goto... commands are ignored. That setting also applies to MS Access itself, and affects all applications you run.

  8. #8
    DaveVlad is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2016
    Posts
    6
    Quote Originally Posted by John_G View Post
    Might you have disabled error handling through the options in the VBA editor? (Tools - Options - General, then Error Trapping set to "Break on All errors?)? If you did, then all On Error Goto... commands are ignored. That setting also applies to MS Access itself, and affects all applications you run.
    Thank you John,

    That sorted it. I didn't think there was anything wrong with my code, as lots of people have asked the same question and the code is generally very similar.

    Regards

    Dave

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

Similar Threads

  1. Runtime Error 2501 on docmd.runSQL
    By schwachmama in forum Access
    Replies: 5
    Last Post: 04-17-2015, 11:28 AM
  2. Replies: 2
    Last Post: 03-13-2014, 09:52 AM
  3. Replies: 2
    Last Post: 08-05-2013, 01:56 PM
  4. runtime error 2501 openform action was canceled
    By rumenrs in forum Programming
    Replies: 2
    Last Post: 04-11-2013, 04:29 AM
  5. Proper Error Trapping
    By SemiAuto40 in forum Programming
    Replies: 6
    Last Post: 08-11-2011, 10:22 AM

Tags for this Thread

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