Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't handle 2501 Error - Report NoData

Status
Not open for further replies.

cjashwell

MIS
Jul 10, 2001
80
GB
In my report_nodata event I have the following code:

Code:
Private Sub Report_NoData(Cancel As Integer)

On Error GoTo Err_handle
Cancel = True

Exit_repnodata:

Exit Sub

Err_handle:

Resume Exit_repnodata

End Sub

And in the code for the form calling the report I have:

Code:
On Error GoTo Err_handle
            DoCmd.OpenReport "rptWeeklyDetails", acViewPreview
        
Exit_cmdWeek:
Exit Sub

Err_handle:
        If Err = 2501 Then
            Err.Clear
        Else: MsgBox Err.Description
        End If
Resume Exit_cmdWeek

End Sub

Yet the 2501 error is still appearing! Can anybody help?
 
I think you need to show Err.Number rather than Err

Code:
Err_handle:
        If Err.[b]Number[/b] = 2501 Then
            Err.Clear
        Else: MsgBox Err.Description
        End If
Resume Exit_cmdWeek

Tom

 
Tom - thanks very much, you are right about that and I've changed that bit of the code. However, this hasn't solved my problem. Basically, the 2501 error is not being handled by the err_handle routine no matter where I put my On Error statement.

I'm going to try a couple of different experiements and I'll report back.
 
OK, I've had another look at this. For test purposes I've set up the following.

frmTest - an unbound form with three command buttons:
cmdRunReport, cmdDeleteData, cmdNewData

This is the code behind the form:

Code:
Option Compare Database

Private Sub cmdDeleteData_Click()

DoCmd.OpenQuery "qryDeleteData"

End Sub

Private Sub cmdNewData_Click()

DoCmd.OpenQuery "qryNewdata"

End Sub

Private Sub cmdRunReport_Click()

On Error GoTo Err_handle
DoCmd.OpenReport "rptTest", acViewPreview

Exit_cmdRunReport:
Exit Sub

Err_handle:
If Err.Number = 2501 Then
    Err.Clear
Else: MsgBox Err.Description
End If
Resume Exit_cmdRunReport
End Sub

There is a report called rptTest. Here is the code behind the report:

Code:
Option Compare Database

Private Sub Report_NoData(Cancel As Integer)

Cancel = True

End Sub

Private Sub Report_Open(Cancel As Integer)

'some code

End Sub

The report is bound to a table, tblWeeklyDetails. When that table contains data the report runs fine. When I delete all the data from the table and try to run the report, I get the run-time error 2501. Stepping in to the code, it seems to have 'ignored' the On Error statement before the DoCmd.OpenReport statement attached to the click event of cmdRunReport.

Can anyone see why this is so - what have I missed?
 
So, after doing some digging, I can't see how to HANDLE the 2501 error at all, but I have discovered that if I set Options|General|Error trapping in the Tools menu to 'Break in Class Module' that allows me to avoid the error altogether.

I'm not entirely happy about this, but it allows me to work-around it for now.

Any feedback very welcome.
 
I created the set exactly as shown, that is, a report and report open code, and it worked as expected. You say "some code" in the report open event, could this be affecting your error code?
 
Hi Remou - 'some code' was literally just a commented line.

When you recreated my code, did the 2501 error actually get handled, or do you have your Error Trapping set to 'Break in Class Module'?
 
It was handled. I have the usual, that is, error handling set to Break on Unhandled Errors. I even stepped through, just to be certain.
 
My Error Trapping settings were 'Break on All Errors'.

'Break on Unhandled Errors' and 'Break in Class Module' are both fine, it's just when it's set to Break on All Errors that this particular error isn't handled.

I'm pretty much OK on this one now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top