Full Version: Error Handling on SendObject email command button
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
snsd
Hi:

I have a command button that simply creates an email and then allows the user to make edits prior to sending it.

CODE
Private Sub cmdEmailExpiryWarning_Click()

DoCmd.SetWarnings True

DoCmd.SendObject , "", "", "emailaddress", "", "", "New Corporate Action" & _
" Expiring within 5 Business Days Alert - Security: " & Me.txtPropertyName, _
"Please be advised that the expiry date for a Corporate Action on " & _
Me.txtPropertyName & " (Security Number: " & Me.txtPropertyNumber & ")" & " will be expiring " & _
"within the next 5 business days. A CCA has been/will be issued today with the details. " & _
"Please do not contact Corporate Actions at this time as the complete CCA has been/will be issued " & _
"today."

End Sub


It works exactly how I want it to work - except when the user decides to cancel the email. In other words, if they close the email without sending it, I get the following error message:

Run-time error '2501';
The SendObject action was canceled.

I'm a bit of a newbie and assume that I simply need to add some code to handle this situation when the user decides not to continue with the email. (BTW, I do not need to force the user to send the email. It is perfectly acceptable for them to cancel the process.)

Any help would be appreciated. (I'm also completely open to a different strategy to process this requirement.

Thanks,

Dave
dannyseager
CODE
Private Sub cmdEmailExpiryWarning_Click()

On Error GoTo next1

DoCmd.SetWarnings True

DoCmd.SendObject , "", "", "emailaddress", "", "", "New Corporate Action" & _
" Expiring within 5 Business Days Alert - Security: " & Me.txtPropertyName, _
"Please be advised that the expiry date for a Corporate Action on " & _
Me.txtPropertyName & " (Security Number: " & Me.txtPropertyNumber & ")" & " will be expiring " & _
"within the next 5 business days. A CCA has been/will be issued today with the details. " & _
"Please do not contact Corporate Actions at this time as the complete CCA has been/will be issued " & _
"today."

next1:
exit sub

End Sub


I can't see why you are setting the warnings to true at the start of your code though..?
snsd
Danny:

Thanks for the quick and perfect response. It works great.

In terms of my setting the warnings to true... I think I copied the code from somewhere on this site, was having a challenge, tried setting it to true, and my challenge was resolved. I just set it to false and it works fine - so I guess setting it to true did nothing in terms of the bug I was experiencing.

From your question, I assume it should be set to false? (Being a newbie, I don't even really understand what it's for - but if it should typically be set to false - don't waste your time trying to explain it to me!)

Thanks again.

Dave thanks.gif
dannyseager
it shouldn't need to be there at all really.

In general the show warnings false/true method is used when running a action query (append/update/delete) but is not the best method then (currentDB.execute "your sql" - is the better method)

You should be able to leave it out... if you take it out and have problems then post the problems on UA and someone will help

Glad to help
snsd
Thanks for the insight Danny:

I removed it and everything runs just fine.

Dave thanks.gif
dannyseager
glad you got it all working
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.