Link to home
Start Free TrialLog in
Avatar of andrewpiconnect
andrewpiconnectFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access 2010 SendObject Cancelled Error

Hi,

I have a form which allows the user to view booking details for any customer.
The user can clcik a button on this form and email the customer a pdf of their booking.
The email button is only visible if the customer has a email address entered in the database.
All works fine except, if the customer has an email address saved in the system and the user clicks the "Email Booking" button but then changes their mind and closes the open email with pdf attachment the system gives a SendObject Cancelled Error and then crashes.

Below is my code. Can anyone assist please?



Private Sub cmdEmailBooking_Click()

On Error GoTo Error_Handler

    Dim stLinkCriteria As String
    Dim strEmailAdd As String

    stLinkCriteria = "[HABooking_ID]=" & Me![HABooking_ID]
    strEmailAdd = Me![EmailAddress]

    DoCmd.OpenReport "rptViewBookingNew", acPreview, , stLinkCriteria
   
    DoCmd.SendObject acSendReport, "rptViewBookingNew", acFormatPDF, strEmailAdd, "myname@domain.net", , "Booking Confirmation", , True
    DoCmd.Close acReport, "rptViewBookingNew"

Error_Handler_Exit:
    Exit Sub

Error_Handler:
    Select Case Err.Number
        Case 2501
            Err.Clear
            Resume Error_Handler_Exit
        Case Else
            MsgBox "Error No. " & Err.Number & vbCrLf & "Description: " & Err.Description, vbExclamation, "Database Error"
            Err.Clear
            Resume Error_Handler_Exit
    End Select

End Sub
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Your error handling looks good.

From memory, SendObject canceled is an error 2501, and your error handler should work with it.

Verify that you dont have Break on All Errors set.

From the VBA editor:

Tools --> Options --> general

Make sure the radio button for "Break on unhandled errors" is selected.
Avatar of andrewpiconnect

ASKER

Hi mb,

Thanks for the reply.

"Break on unhandled errors" was already selected. So I'm still  stuck?
Try setting a few breakpoints to see where the code execution is falling apart-

- on the sendObject
- Right after the Send Object
- in the error handler

If it makes it to the error handler, check the value of Err.Number (you can do this by hovering the mouse over Err.Number in the code)


<< the system gives a SendObject Cancelled Error and then crashes. >>
Also, is this your custom error message which is generated by the error handler or is it Access's built in message with the "debug" and "end" buttons?
How do you enter a breakpoint??

I shall chase to see if i can get a screenshot of the error msg over to you.

It might be worth adding that I get no errors when i run the system on my development pc. The error is occuring on the users pc's
<< How do you enter a breakpoint?? >>

Click in the left margin on any executable line of code.  You'll see a red not next to that line, and the code will stop during execution at that line (F5 to continue).  If you're not familiar with debugging like that you may want to look for a quick online tutorial.


<<
It might be worth adding that I get no errors when i run the system on my development pc. The error is occuring on the users pc's
>>

That said, before going through that effort, verify that Break on Unhandled Errors is set on your users' PCs as well.

(And get a screenshot of that error from one of their PCs)
Ok thanks,

I will revert back as soon as I get the screenshot form the users pc. I have chased for this just now.
One other point.  Why are you opening the report before sending it, you don't need to do that, and with the way you have your error handler setup, the code that closes the report will not be executed if you cancel the email.

I would recommend:

1.  Getting rid of the two lines that open/close the report, OR
2.  Getting rid of the SendObject line and Close report line from that segment of code.  Then create a report shortcut menu (see attached) and use it in your Report.  This is the way I deal with all my reports.  That way, I can view the  report and subsequently decide what I want to do with it.

If you call the MenuReport subroutine when you load your application, you can then set the reports "Shortcut Menu Bar" property to "MyReportMenu".  From then on, you can right click on the report in PrintPreview mode to either Print, SaveAsPDF, SendPDF, or Close the report.

Option Compare Database
Option Explicit

Const BarPopup = 5
Const ControlButton = 1
Const ControlEdit = 2
Const ControlComboBox = 4
Const ButtonUp = 0
Const ButtonDown = -1

Public Sub MenuReport(Optional Reset As Boolean = False)

    Dim cbr As Object       'As CommandBar
    Dim cbrButton As Object
    Dim cbrCombo As Object   'CommandBarComboBox
    Dim cbrCombo1 As Object, cbrCombo2 As Object
    Dim cbrEdit As Object
    Dim strSQL As String
    Dim rs As DAO.Recordset

    On Error GoTo ReportMenuError

    DoCmd.Hourglass True

    Set cbr = CommandBars.Add("MyReportMenu", BarPopup, , True)

    With cbr

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "&Print"
            .Tag = "Print"
            .OnAction = "=fnReportPrint()"
        End With

        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "Save as &PDF"
            .Tag = "Save as PDF"
            .OnAction = "=fnReportSave('PDF')"
        End With
        
        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "Send as &PDF"
            .Tag = "Send as PDF"
            .OnAction = "=fnReportSend()"
        End With
            
        Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
        With cbrButton
            .Caption = "&Close"
            .Tag = "Close"
            .OnAction = "=fnReportClose()"
            .BeginGroup = True
        End With

    End With

    DoCmd.Hourglass False
    Exit Sub
ReportMenuError:
    MsgBox Err.Number & vbCrLf & Err.Description, , "ReportMenu error"
End Sub
Public Function fnReportPrint()

    Dim rpt As Report, strRptName As String
    Dim strMsg As String
    Dim intResponse As Integer, bPrint As Boolean

    On Error GoTo PrintReportError

    Set rpt = Reports(Reports.Count - 1)
    strRptName = rpt.Name
    
    bPrint = True

    If rpt.Pages > 10 Then
        strMsg = "This report contains " & rpt.Pages & " pages! " _
               & vbCrLf & vbCrLf _
               & "Print this report anyway?"
        intResponse = MsgBox(strMsg, vbOKCancel, "Excessive pages")
        If intResponse = vbCancel Then bPrint = False
    End If

    If bPrint Then
        With rpt
            Application.RunCommand acCmdPrint
        End With
    End If

    Exit Function

PrintReportError:
    If Err.Number = 2501 Then
        'do nothing (print was cancelled)
    Else
        msgbox "Error in fnReportPrint"
    End If

End Function
Public Function fnReportSave(OutputFormat As String)

    Dim rpt As Report
    
    On Error GoTo SaveReportError
    
    If Reports.Count = 0 Then
        Exit Function
    Else
        Set rpt = Reports(Reports.Count - 1)
    End If
    
    DoCmd.OutputTo acOutputReport, rpt.Name, "PDF Format (*.pdf)", , True
    
SaveReportExit:
    Exit Function

SaveReportError:
    If Err.Number = 2501 Then
        Exit Function
    ElseIf Err.Number = 2282 Then
        MsgBox WrapText("Your system does not currently have the ability to save a file " _
                      & "in a PDF format." & vbCrLf _
                      & "Contact your system administrator to request addition of this " _
                      & "functionality to your suite of MS Office tools!", 65)
    Else
        Msgbox "Error encountered while printing report"
    End If
End Function
Public Function fnReportSend()

    Dim cbr As Object
    Dim cbrCombo As Object
    Dim strFormat As String
    Dim rpt As Report, strReport As String
    Dim rs As DAO.Recordset
    
    Set rpt = Reports(Reports.Count - 1)
    strReport = rpt.Name
    strFormat = "PDF Format (*.pdf)"
    
    On Error GoTo ReportSendError
    
    DoCmd.SendObject acSendReport, strReport, strFormat, , , , "Subject", , True
    
ReportSendExit:
    Exit Function
    
ReportSendError:
    If Err.Number = 2501 Then
        'MsgBox "Send email was cancelled!"
    ElseIf Err.Number = 2282 Then
        MsgBox WrapText("Your system does not currently have the ability to save a file " _
                      & "in a PDF format." & vbCrLf _
                      & "Contact your system administrator to request addition of this " _
                      & "functionality to your suite of MS Office tools!", 65)
    Else
        MsgBox "Error encounterd during fnSendReport!"
    End If
    
End Function
Public Function fnReportClose()

    Dim rpt As Report
    Dim strMsg As String
    Dim intResponse As Integer, bPrint As Boolean

    On Error GoTo fnReportCloseError
    
    If Reports.Count > 0 Then
        DoCmd.Close acReport, Reports(Reports.Count - 1).Name
    End If
    
    Exit Function

fnReportCloseError:
    If Reports.Count > 0 Then strMsg = " Report: '" & Reports(Reports.Count - 1).Name & "'"
    strMsg = "Error encountered in fnCloseReport():" & strMsg
    Msgbox strMsg

End Function

Open in new window

Dale,

I believe the Open Report statement is there so that a filtered copy of the report gets sent.   Without that, SendObject would include the report with all records.

There are other approaches of course such as including the criteria in the report, but I believe his approach with the error handling should work okay.
Have to agree with MB here.

I need a report to open to a filtered copy, otherwise it would be multiple pages long containing all records.

I ma still waiting for the screenshot of the error from the user as I feel this may be an issue with the user pc and not my code as it works without any erros on my pc.
Miriam,

You are absolutely right, I missed the criteria, it must be that I have not had my coffee yet.

In that case, I would strongly recommend moving the Docmd.Close down, so that it looks like:

Error_Handler_Exit:
    DoCmd.Close acReport, "rptViewBookingNew"
    Exit Sub

That way, the report will get closed if the SendObject is cancelled.
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MB,

Yep I saw your suggestion and have someone checking they have "Break on unhandled errors" set.

Default printer is definitley set but handy to know all the same.

I feel pretty confident this is the route to success!