Run-time error 2501; OpenReport action was cancelled (1 Viewer)

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
Folks,
Need your help. I searched the entire forum and found couple of postings on this, but none of them indicates if there is any fix for this problem.

If there is no data, basically I want to show a message and close the report. I have some code to handle this under no data event like below:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report. Canceling report..."
Cancel = True
End Sub


But this 2501 always pops up and I couldn't find a way to stop this. I also searched Microsoft knowledgebase, no help.
Here's how my clik event procedure looks like:
Private Sub cmdSearch_Click()
On Error GoTo Err_PreviewRprt
Dim strTbl As String

Dim strSQL As String

Dim strOptItem As String
Dim strOptBlrType As String

Dim strMsg As String, strTitle As String
Dim intStyle As Integer


'Set up SQL statement for report record source
If IsNull(optCriteria) Then optCriteria = 1
strTbl = Me.cboTypeOfGuar.Column(0)
strOptItem = Choose(optCriteria, "memPred", "memGuar", "memRiskLevel", "memLDs")

strOptBlrType = Choose(optBoilerType, "SWUP", "RB", "CFB", "All")



strSQL = "SELECT tblProjts1.chrProjectName, tblProjts1.chrBlrPropNum, tblProjts1.chrBoilerType, " & _
strTbl & ".memGuranItem, " & strTbl & "." & strOptItem & _
" FROM tblProjts1 INNER JOIN " & strTbl & " ON " & _
"tblProjts1.intProjectId = " & strTbl & ".intProjectId" & _
" WHERE (((" & strTbl & ".memGuranItem)" & " IS NOT NULL)" & " AND " & "((tblProjts1.chrBoilerType) = '" & strOptBlrType & "'))"


'Open report in Design view to set the report's record source
'and search item label's caption
DoCmd.Echo False 'suppress the screen updates


DoCmd.OpenReport "rptBlrSrchItem1", acViewDesign
With Reports("rptBlrSrchItem1")
.RecordSource = strSQL
.Controls("strOptItem").ControlSource = Choose(optCriteria, "memPred", "memGuar", "memRiskLevel", "memLDs")

End With

DoCmd.Close , , acSaveYes

'Now show the search results to the user
DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview

DoCmd.Echo True

Exit_PreviewRprt:
Exit Sub

Err_PreviewRprt:
If Err = 2501 Then
Resume Exit_PreviewRprt
Else
MsgBox Err.Description
Resume Exit_PreviewRprt
End If

End Sub


Any help is much appreciated.
 

boblarson

Smeghead
Local time
Today, 05:22
Joined
Jan 12, 2001
Messages
32,059
You have to capture the error in the code that opens your report as that's the code that is generating the error, not the cancel of the no data. I usually put in an error handler and trap for error 2501 to basically tell it to ignore that error (exit sub) when that error occurs. But the confusing part that got me initially was that I was trying to figure out what code generated it. It is the DoCmd.OpenReport code as it isn't finishing since the open gets canceled before the opening code finishes.
 

boblarson

Smeghead
Local time
Today, 05:22
Joined
Jan 12, 2001
Messages
32,059
I just noticed that your trap is slightly off:

Change
Code:
If Err = 2501 Then
Resume Exit_PreviewRprt
Else
MsgBox Err.Description
Resume Exit_PreviewRprt
End If
to this
Code:
If Err[B].Number[/B] = 2501 Then
    Resume Exit_PreviewRprt
Else
    MsgBox Err.Description
    Resume Exit_PreviewRprt
End If
 

Wiz47

Learning by inches ...
Local time
Today, 08:22
Joined
Nov 30, 2006
Messages
274
ShanVel said:
Folks,
Need your help. I searched the entire forum and found couple of postings on this, but none of them indicates if there is any fix for this problem.

If there is no data, basically I want to show a message and close the report. I have some code to handle this under no data event like below:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report. Canceling report..."
Cancel = True
End Sub


But this 2501 always pops up and I couldn't find a way to stop this. I also searched Microsoft knowledgebase, no help.
Here's how my clik event procedure looks like:
Private Sub cmdSearch_Click()
On Error GoTo Err_PreviewRprt
Dim strTbl As String

Dim strSQL As String

Dim strOptItem As String
Dim strOptBlrType As String

Dim strMsg As String, strTitle As String
Dim intStyle As Integer


'Set up SQL statement for report record source
If IsNull(optCriteria) Then optCriteria = 1
strTbl = Me.cboTypeOfGuar.Column(0)
strOptItem = Choose(optCriteria, "memPred", "memGuar", "memRiskLevel", "memLDs")

strOptBlrType = Choose(optBoilerType, "SWUP", "RB", "CFB", "All")



strSQL = "SELECT tblProjts1.chrProjectName, tblProjts1.chrBlrPropNum, tblProjts1.chrBoilerType, " & _
strTbl & ".memGuranItem, " & strTbl & "." & strOptItem & _
" FROM tblProjts1 INNER JOIN " & strTbl & " ON " & _
"tblProjts1.intProjectId = " & strTbl & ".intProjectId" & _
" WHERE (((" & strTbl & ".memGuranItem)" & " IS NOT NULL)" & " AND " & "((tblProjts1.chrBoilerType) = '" & strOptBlrType & "'))"


'Open report in Design view to set the report's record source
'and search item label's caption
DoCmd.Echo False 'suppress the screen updates


DoCmd.OpenReport "rptBlrSrchItem1", acViewDesign
With Reports("rptBlrSrchItem1")
.RecordSource = strSQL
.Controls("strOptItem").ControlSource = Choose(optCriteria, "memPred", "memGuar", "memRiskLevel", "memLDs")

End With

DoCmd.Close , , acSaveYes

'Now show the search results to the user
DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview

DoCmd.Echo True

Exit_PreviewRprt:
Exit Sub

Err_PreviewRprt:
If Err = 2501 Then
Resume Exit_PreviewRprt
Else
MsgBox Err.Description
Resume Exit_PreviewRprt
End If

End Sub


Any help is much appreciated.

Just add an error handler to the code you use to execute the report:

Something like:

Code:
Private Sub Open_Report_Click()

On Error GoTo Err_Report_Click

    DoCmd.OpenReport Me.Report, ac_Preview

Exit_Report_Click:
    Exit Sub

Err_Report_Click:
    If Err.Number = 2501 Then
        Resume Next
    Else

        MsgBox Err.Description
        Resume Exit_Report_Click
    End If
End Sub
 
Last edited:

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
boblarson,
Thanks for catching up the mistake.

I fixed the line to Err.Number, still same message Error 2501

When I click 'Debug' button it always takes to this line of code:
DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview
 

boblarson

Smeghead
Local time
Today, 05:22
Joined
Jan 12, 2001
Messages
32,059
Okay, something isn't happening right. You should not be getting a debug button given that your error handler doesn't give a dialog with that option. That tells me that it is happening on the code that doesn't have an error handler. I would put a breakpoint in a few lines above your code where it is highlighting and then step through it, a line at a time, when it breaks so that you can see exactly when it is generating the errror. It is at that point where you will need to put another error handler.
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
Wiz47 said:
Just add an error handler to the code you use to execute the report:

Something like:

Code:
Private Sub Open_Report_Click()

On Error GoTo Err_Report_Click

    DoCmd.OpenReport Me.Report, ac_Preview

Exit_Report_Click:
    Exit Sub

Err_Report_Click:
    If Err.Number = 2501 Then
        Resume Next
    Else

        MsgBox Err.Description
        Resume Exit_Report_Click
    End If
End Sub

Wiz47,
I did try this and same err 2501 messgae. Looks like there is no way to trap this error. I tried to trap this on the report's "on close, on error, on open, on no data" and nothing seems to work.
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
bolarson,

I added a breakpoint just above the following line and step thru line by line.
"DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview"

This time I didn't get the 2501 messgae with 'End' and 'Debug' button, but just when it came to that line it crashed.

Also, I tried one more error trap as follows and no use.
On Error Resume Next
DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview


Strange, I spent almost a day and we can't trap an error which has an error code by MS Access 2003.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:22
Joined
Aug 30, 2003
Messages
36,124
That error is commonly trapped, and your code should trap it. I'm shooting from the hip, but try modifying the

DoCmd.Close

to explicitly close the report.
 

Wiz47

Learning by inches ...
Local time
Today, 08:22
Joined
Nov 30, 2006
Messages
274
ShanVel said:
Wiz47,
I did try this and same err 2501 messgae. Looks like there is no way to trap this error. I tried to trap this on the report's "on close, on error, on open, on no data" and nothing seems to work.

That is strange. I use this errorhandler to trap the option to cancel when someone backs out of various procedures - so the CANCEL msgbox doesn't pop up - it just exits the sub.

I use the same procedure (tweaked a bit and uses a different err.number) to handle no data in a report. I use it on the onload event for the report (I have it send a message that the report does not have data, then it allows the user to exit the sub by clicking OK). I thought that sending a message on a "No Data" event was better than exiting the sub without letting them know why.
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
Wiz47,
Can you give me some more detailed explanation? You are talking about load event of a report? I don't see that event listed for a report?
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
I am back on the same issue.

Dou you think using a macro to open the report instead of Docmd (DoCmd.OpenReport "rptBlrSrchItem1", acViewPreview) would help? I just don't know how to do this inside my click event procedure. Your help is greatly appreciated.
 

boblarson

Smeghead
Local time
Today, 05:22
Joined
Jan 12, 2001
Messages
32,059
If you can post your database, we can track down the problem. There's obviously something not being communicated and sometimes it's hard to know what to ask when seeing can give the answer immediately (especially for me as I'm very visual).
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:22
Joined
Aug 30, 2003
Messages
36,124
Personally, no. Did you try my shot-in-the-dark in post 9? I always trap empty reports by setting cancel = true in the no data event, and trapping 2501 in the code that opened it. You have something weird going on. Can you post a sample db?
 

Wiz47

Learning by inches ...
Local time
Today, 08:22
Joined
Nov 30, 2006
Messages
274
ShanVel said:
Wiz47,
Can you give me some more detailed explanation? You are talking about load event of a report? I don't see that event listed for a report?

Sorry for not responding sooner, I was working on something else. I meant to say the OnOpen event - I didn't mean to add to the confusion. I would take BobLarson's and PBaldy's advice and upload a sample of the Db. You have two of the best here willing to help. I'd certainly jump at that opportunity if I had a problem.
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
pbaldy, boblarson & Wiz47,
Thanks for all your time and sorry to respond to your request late.

Note to pbaldy: I did exactly what you have described on your posting, no help.

I will definitely get back with a sample DB, but before that here's what I tried:
1. I tried by creating a macro to open the report and then converted that to VBA code and copied and pasted in my click-event procedure. No use, same 2501 message.

2. I installed the database on two different laptops with my colleagues. It works perfectly fine. After no data message, when I click the OK button it takes me to the form which has the command button for the click event. Whereas in my laptop, after no data message, when I click 'OK' button, Access simply crashes.

Any idea why this is happening only on my machine? On some posts from other web sites they have described that print drivers or printer port might cause this problem. Any ref to some postings also might help.

Folks - Thanks, again, for willing to spare some time on my request.
 

Wiz47

Learning by inches ...
Local time
Today, 08:22
Joined
Nov 30, 2006
Messages
274
ShanVel said:
... Any idea why this is happening only on my machine? On some posts from other web sites they have described that print drivers or printer port might cause this problem. Any ref to some postings also might help.

Folks - Thanks, again, for willing to spare some time on my request.

I've read that in the forums here as well. I'd do a quick search for something like "Report crashes" or "Report crashes when loaded" and see what pops up. I recall them saying that if the printer or printer driver is not recognized or loaded properly - it will crash when trying to run a report.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:22
Joined
Aug 30, 2003
Messages
36,124
I feel stupid, cause I knew this somewhere deep down. In the VBA editor, go to Tools/Options. On the general tab, make sure Error Handling is set to Break on Unhandled Errors. My guess is it's set to Break on All Errors.
 

ShanVel

ShanMug
Local time
Today, 08:22
Joined
Oct 12, 2005
Messages
51
Paul,

Thanks for the response. You are correct; it was set to Break on All Errors.
I changed to "Break on Unhandled Errors"

Now, I am not getting 2501 error msg but when I click OK after no data message it takes me back to the same form which has a search button. Then the program simply hangs and crashes.

I have no clue, we fixed one issue now how to stop the program being hangs out and crashes. It didn't happen on my colleagues PC.
 

Users who are viewing this thread

Top Bottom