Need to disappear this dialog box 'Runtime Error 2501' (1 Viewer)

Ashfaque

Student
Local time
Tomorrow, 04:23
Joined
Sep 6, 2004
Messages
894
Hi,

After executing below code successfully, my email is ready to send.

But in case if I dont want to send the email so I am can cancel or stop sending the email using its X button in outlook. But after than on my form the said dialog box of Run-time error 2501 appearing. And after closing this dialog box, db is running is running smoothly. I want this dialog box not appear.

Code Tags Added by UG
Please use Code Tags when posting VBA Code

https://www.access-programmers.co.u...e-use-code-tags-when-posting-vba-code.240420/
Code:
Dim strMailList, StrCC As String

strMailList = CEmail
StrCC = "ifonat@gmail.com"

DoCmd.SendObject acSendReport, "R_JobOffer", acFormatPDF, strMailList, StrCC, , "JOB OFFER LETTER - " & LetterRefNumber & _
" - " & CName & " - " & Format(Now()), "Dear " & CName & "," & vbCr & vbCr & "With reference to our.....  & _
"''''''accept this offer, please sign and re-send back to us as your acceptance." & vbCr & vbCr & "Good Luck and Best Regards," & _
vbCr & vbCr & "Sincerely," & vbCr & vbCr & "Brian Clark" & vbCr & "Human Resources Dept.", True

End If
And after email cancelation, the screen is stuck up and unable to do anything except to close it thru task manager. The msg appears the need to terminate the module.
Any help shall be appreciated.

Ashfaque
 
Last edited by a moderator:

Ranman256

Well-known member
Local time
Today, 18:53
Joined
Apr 9, 2015
Messages
4,339
For the first step use:
On error résumé next
 

Ashfaque

Student
Local time
Tomorrow, 04:23
Joined
Sep 6, 2004
Messages
894
While I am trying to close the whole db using task manager, it displays msg in between that :

"If you are running a Visual Basic Module that is using OLE or DDE, you may need to interrupt the module"

Please help...
 

Ashfaque

Student
Local time
Tomorrow, 04:23
Joined
Sep 6, 2004
Messages
894
Thanks Ranman,
Yes, I already used as below :

On Error GoTo Err_CmdSendEmail_Click
......
...........
..............
................
Exit_CmdSendEmail_Click:
Exit Sub

Err_CmdSendEmail_Click:
MsgBox Err.Description
Resume Exit_CmdSendEmail_Click
 

Micron

AWF VIP
Local time
Today, 18:53
Joined
Oct 20, 2018
Messages
3,476
Yes, I already used as below :

On Error GoTo Err_CmdSendEmail_Click
That is not what was suggested.
Dim strMailList, StrCC As String

On Error Resume Next
strMailList = CEmail
StrCC = "ifonat@gmail.com"

However, I would not do it that way either. Handle 2501 in your error handler. I cannot write a suggestion because your code is incomplete and the 2 parts you've shown are not even the same thing. If you want help with that, post the entire sub - and please use code tags (see thread toolbar). Also, Dim your variables explicitly when doing so on one line because the way you have it, strMailList is a Variant, not a string.
Dim strMailList As String, StrCC As String
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:53
Joined
Feb 28, 2001
Messages
27,001
I will amplify what Micron said just a little.

The reason you got the error was because of what is called an "unhandled error trap" that is part of the Access VBA environment. Remember that you run code in your Access app because of an event routine (which is always a subroutine) or a RunCode macro (which must call a function).

In any code segment, you are in a subroutine (or function) context for which you can declare an On Error GoTo xxx statement where xxx is the code label for your error handler in that code. You probably knew about that. But what you might not have known was what happens if you DON'T supply the On Error statement.

In that case where you have not declared a handler, Access CANNOT allow an unhandled error to exist. Errors are a type of program interrupt (in many cases) and they stop your code execution to enter the error handler context. This context is NOT the same as a subroutine call from the VBA viewpoint because you have to leave that context using RESUME rather than EXIT SUB. So to assure proper error handling, Access provides a default handler.

Access can tell by looking at something called the "Call Frame" whether you have declared a handler. If you have not, your unhandled error in the subroutine forces VBA to abort execution of your subroutine/function and return to the caller of that routine. Remember that if that code was called as an EVENT routine or a RunCode macro, the caller was Access itself.

That situation is when you get that error handler box. That is also why you can't do anything else - because when the "last chance" error handler kicks in, your code is no longer active. That is why your screen gets "stuck" - because you are no longer able to run code until you reset the app context to allow you to proceed.

The solution is that you can trap errors in your routine. Then in the error handler, you can do a case statement or an IF-THEN-ELSE-ENDIF sequence so that you can look at Err.Number and if it is 2501, you can choose to do nothing except perhaps a Resume Next. If you use a wizard to create an event routine, that wizard will often create a MsgBox notification. If it does that, you can use that as a starting point for handling your own errors. Your strategy would then depend in finding a way to "clean up" after that situation triggers your 2501 error.

The benefit of handling your own errors is that you remain in control of what is happening because YOU can choose the action that occurs after errors.
 

Ashfaque

Student
Local time
Tomorrow, 04:23
Joined
Sep 6, 2004
Messages
894
Excellent clarification The_Doc_Man,

Thanks you very much.....(y)

With warm regards,
Ashfaque
 

Users who are viewing this thread

Top Bottom