[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access 2010 SendObject Cancelled Error

Posted on 2012-09-17
12
Medium Priority
?
1,744 Views
Last Modified: 2012-09-27
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
0
Comment
Question by:andrewpiconnect
  • 5
  • 5
  • 2
12 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38404953
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.
0
 

Author Comment

by:andrewpiconnect
ID: 38404966
Hi mb,

Thanks for the reply.

"Break on unhandled errors" was already selected. So I'm still  stuck?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38404978
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:andrewpiconnect
ID: 38404995
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
0
 
LVL 61

Expert Comment

by:mbizup
ID: 38405007
<< 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)
0
 

Author Comment

by:andrewpiconnect
ID: 38405022
Ok thanks,

I will revert back as soon as I get the screenshot form the users pc. I have chased for this just now.
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 38405132
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

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38405138
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.
0
 

Author Comment

by:andrewpiconnect
ID: 38405153
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.
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 38405154
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.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 38405169
<<
I feel this may be an issue with the user pc and not my code as it works without any erros on my pc.
>>

Not sure if you caught my earlier suggestion, but do verify that they have "Break on unhandled errors" set (that is a setting that applies to individual PCs).

Also make sure that they have a default printer set.  Even if they are simply sending the report, it will fail with error 2501 if there is no printer.  But that is a long shot, which I think would make your code fail in all cases on their computers - not just when they click cancel.
0
 

Author Comment

by:andrewpiconnect
ID: 38405188
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!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question