Error 2501 when print cancelled. (1 Viewer)

Phredd

Registered User.
Local time
Tomorrow, 05:41
Joined
May 28, 2002
Messages
34
I know this has been covered to death, and I think I have read every post.

My issue is although I think I am following all the advise, it appears my line
"On Error GoTo Errorhandler" is not being recognized.

I have put this line in many locations, and every time it just seems to skip or ignore it. :banghead:

Any ideas guys ? I have edited and include the code below...

-------------------
Private Sub Command300_Click()

On Error GoTo Errorhandler

If IsNull([Date Summary Sent]) Or IsNull([CompDate]) Or IsNull([Date Discharge]) Then
MsgBox "Please ensure you have Entered 'Date Sent', 'Completion Date', 'Discharge Date' and 'Name'."
Else
If Me.Dirty Then
Me.Dirty = False
End If

If [Discharge Medication] = True Then
On Error GoTo Errorhandler
strWhere = "[recordnumber] = " & Me.[RecordNumber]
DoCmd.OpenReport "Print Report", acViewPreview, , strWhere
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "Print Report", acSaveNo
DoCmd.OpenReport "Rpt-Medicationform", acViewPreview, , strWhere
MsgBox "There is a Medication Summary Form attached to this report. Please select Printer & Number of copies required Then print"
On Error GoTo Errorhandler
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "Rpt-Medicationform", acSaveNo

Else
strWhere = "[recordnumber] = " & Me.[RecordNumber]
DoCmd.OpenReport "Print Report", acViewPreview, , strWhere
DoCmd.RunCommand acCmdPrint
DoCmd.Close acReport, "Print Report", acSaveNo

End If

Errorhandler:
If Err.Number = 2501 Then
MsgBox "You cancelled the print"
End If

End If
End Sub
---------------------------
 
Last edited:

Phredd

Registered User.
Local time
Tomorrow, 05:41
Joined
May 28, 2002
Messages
34
The "Error" handle ??

Not sure what "Hitting" it will do. Nothing seems to has changed regarding the error.

Remove all error handling or leave as is. Either way it errors 2501 ever time I cancel the print.
 

GinaWhipp

AWF VIP
Local time
Today, 15:41
Joined
Jun 21, 2011
Messages
5,899
If you are sure that it is not a problem with the Report you will need to trap that error.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:41
Joined
Sep 12, 2006
Messages
15,710
2 issues.

first as paul said, your code will drop into the error handler every time, irrespective of whether it errors or not.

other than that, you are testing only for error 2501, which is paraphrased as "report or form failed to open". Cancelling a report does generate error 2501, as the report fails to open in that case. If your error is not caused by the report failing to open you will get a different error, or maybe no error

it's hard to know exactly what you are trying to do. I would either step through the code, or alternatively put msgboxes after each statement to trace the process execution.

A lot of this is really understanding process construction. eg - I am not sure whether the sequence to open a report in preview mode, print it, and then close does exactly what you expect. It may do, but it's not a construct I use.

looking again, I can't follow the structure of the code. The final "end if" seems to be an orphan? It's hard to tell without the code being indented. It's not clear, but checking more carefully the error handler seems to be nested inside the whole of a block of code starting with me.dirty bit at the top? Is that right?

-----------------
I re-read the thread, and my reply, I don't want to delete it, as I have spent a few minutes on it, but it's a bit garbled - so I will try again.


So

a) cancelling a report or form opening for any reason generates error 2501. either because you click cancel, or you cancel it opening for no data, or even because of a logic error that means the report's source query fails.

b) as paul pointed out, your code drops into the error handler even if it works normally, although at that point you won't have error 2501. It's not the right way to construct an error handler though, in my opinion. You should only jump to the error handler in the event of an error.
 
Last edited:

Users who are viewing this thread

Top Bottom