Error 2051 - Cancel SendObject

patkeaveney

Registered User.
Local time
Today, 22:01
Joined
Oct 12, 2005
Messages
75
I can't seem to successfully deal with this error.

The scenario is as follows. I call the above routine when the user clicks on a save button I have added to the form, which launches an email. with the option of having the User see it first. That works fine.

If the user chooses to not send the email however, and hits the email's close button to Cancel the send, I get Error 2051 popping up in Access.

I built code to trap the error, but it is not working. The error process is not being reached.

Can someone plese help.

Private Sub SendConfirmationEMail()
On Error GoTo SendConfirmationEMail_Err

Dim strTo As String
Dim strCC As String
Dim strSubject As String
Dim strMsgBody As String

EMailOK = False

strSubject = "FOI Request Confirmation - " & Me.txtIntRefCode

If Len(Me.ReqEmailAddress & vbNullString) = 0 Then
MsgBox "Cannot Send EMail" & vbCrLf & vbCrLf _
& "No E-mail Address Entered for " & vbCrLf & vbCrLf _
& cmbRequestorName.Column(1), vbExclamation, "E-Mail not Sent"
Exit Sub​
Else
strTo = Me.ReqEmailAddress
strCC = "patrick.keaveney@somepct.nhs.uk"
strMsgBody = vbCrLf & _
Me.cmbRequestorName.Column(3) & vbCrLf & vbCrLf _
& "Many thanks for your request for information under the freedom of information act." & vbCrLf & vbCrLf _
& "I acknowledge receipt of your request and confirm that this is now receiving action." & vbCrLf _
& "A response will be sent to you in due course" & vbCrLf & vbCrLf _
& "Please note the reference number for your request is " & Me.txtIntRefCode & vbCrLf & vbCrLf _
& "Kind Regards"

DoCmd.SendObject , , , strTo, strCC, , strSubject, strMsgBody, True
End If
EMailOK = True

Exit_SendConfirmationEMail:
Exit Sub

SendConfirmationEMail_Err:

Select Case Err.Number

Case 2501 'Traps Error 2501
MsgBox "E-Mail Was Cancelled", , "Cancelled"
Resume Exit_SendConfirmationEMail

Case Else
MsgBox "Other Error" & Err.Description 'Will show every other error
Resume Exit_SendConfirmationEMail

End Select
End Sub
 
You have to trap it at the source. Meaning trap error 2501 in the save button's "click" procedure.
 
Case 2501 'Traps Error 2501
MsgBox "E-Mail Was Cancelled", , "Cancelled"
Resume Exit_SendConfirmationEMail

at the bottom you have this code

cancelling the email ACTUALLY GENERATES error 2501, and then your code displays this error message. if you dont want any message (which you probably dont), just comment out the msgbox line

note that if you DIDNT have this error handler, then cancelling the report/email would crash the program (you know the end/debug messagebox)

i think we tend to add 2501 traps while developing, then remove them for production
 
i think we tend to add 2501 traps while developing, then remove them for production

I really don't understand the need for the error. So I usually handle it in this method:
Code:
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

A prime example is if I use a button to open a report, but in the report I cancel the report because of no data. I already know I am canceling it so why have an error message? Just a little peeve of mine :)
 
thanks for the replies.
gemma: i put the msgbox in to see if the error process was working:
Irish: i need to trap the error, because at the moment when the email send is cancelled the run time error message is being displayed and the code is highlighted on the DoCmd.SendObject line in the sub above.
Do i need to move all this code into the On click of the save button.
does not seem logical as the code stops in the sub.
But i will bow to your superior knowledge, if this is the case.
always willing to accept help.

thank you both
 
it looks like it is in the right place to me - so you wouldnt need to move it - But are you saying it isnt doing what you expect?
 
Normally when you wind up with a 2501 Action has been canceled error you would capture that error at the event which started everything off. So, I do believe in this case you would need to put it on the click event which starts it off.

This is much like when you call a report which has a No Data event and you set Cancel=True in that event, but you still need to capture for the 2501 error in the event which called the report to begin with.
 
exactly,the access runtime error message 2501 is being displayed.
 
i've deleted my last post

if its a 2501 error on the click event, why would the debug still goto to the send object line.

it looks to me like the error handler in here should work fine.

I would put a breakpoint in that block and trace the code execution

---------------
 
Last edited:
Normally when you wind up with a 2501 Action has been canceled error you would capture that error at the event which started everything off. So, I do believe in this case you would need to put it on the click event which starts it off.

This is much like when you call a report which has a No Data event and you set Cancel=True in that event, but you still need to capture for the 2501 error in the event which called the report to begin with.

Thanks for the concurrence. :D

Do i need to move all this code into the On click of the save button.
does not seem logical as the code stops in the sub.
Not so sure my knowledge is superior :p but yes, put it in the save button click procedure. You may be surprised at the result.
 
Last edited:
thanks bob and irish

I will move the code and let you know.
I am used to putting code in subs for ease of programming. this is a new one on me
 
just tried the original code (error processing in sub)on my home pc running outlook 2007 and 2051 error processing worked ok, my error message was displayed
I will rerun it at work on monday where pc is running outlook 2003

Strange
 
moved the code into the save_click event as suggested above , but error preocessing still not being triggered. access displays runtime error 2501 and halts the debug msgbox appears
This is back at my works computer, as per original post.

Any other suggestions please
 
perhaps you cant sendobject to use email at work - wrong email client, or login privileges issue or something
 
Hi Gemma
I can send email, if the user clicks send instead of cancel the email is sent successfully
thanks for your suggestion.
 
if you select create foi request from main menu
on status tab
enter an internal ref code format (999-09)
select a received date and a date to be completed by
on requestor tab
select "daily mail" from organistaion drop down
Select White, Paul from Requestor name drop down

click Save FOI request.

a prompt will asking you to save request, reply Yes and a promt asking if you want to send an email will appear
again press yes and email screen will appear.

EMail process will be run.

Thanks again for your help
 

Attachments

i couldnt get it to run properly, in the format in which you posted it - eventually i copied the add form to an amend form and tweaked it to work ok


but for me - the error handler worked just as you expected - if you close the email without sending, the handler in the send confirmation code just kicks in with error 2501.

i really think there must be some issue with your work computer affecting this, but I can't think what it is.

-----------
except - if you have encountered an earlier error, in your code and your error handler for THAT error DOESNT have a resume statement, then your code continues to run, but now you are still in the error handler for THAT exception, and you cannot activate a NEW error handler - so might this be the cause? - ie the email error handler is NOT being set
 
Last edited:
Err_Form_BeforeUpdate:
If Err = 3020 Then 'Update or CancelUpdate without AddNew or Edit
Exit Sub
Else

eg, theres one in formNewRequest - this will not reset the error handler, and you wont be able to set a new one, because there is no resume statement.

I better send the NHS a big bill now - they can afford loads!
 
Last edited:
Hi Gemma
the cheques in the post LOL

i added a resume statement, but still no joy, what adjustmenys did you have to make, do i need to change my code.
I am still baffled as to why the code runs ok on my home computer, and the send element works fine at work
 

Users who are viewing this thread

Back
Top Bottom