Can't trap 2501

Mina

New member
Local time
Yesterday, 19:26
Joined
Oct 14, 2004
Messages
5
I am using version 2003 and here's the scenario:

I am having the devil of a time trying to prevent the following message from popping up when I attempt to open a report with no data. To make my explanation easier, I created a test button and put simple code behind it that duplicates my situation. Here it is:

Private Sub cmdErrorTrapTest_Click()

On Error GoTo ErrorHandler

DoCmd.OpenReport "rptEquipmentMalfunctionsCustom", acViewPreview


ExitHandler:
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
Resume ExitHandler

Else
MsgBox Err.Description
Resume ExitHandler

End If
End Sub

Here's the code behind the "NoData" event of the report:


Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ErrorHandler

MsgBox "There are currently no records", vbOKOnly,"No Data"
Cancel = True

ExitHandler:
Exit Sub

ErrorHandler:
MsgBox Err.Description
Resume ExitHandler

End Sub

No matter how I manipulate the code, I can't seem to kill the 2501 message. I know I must be doing something ridiculously silly, and not aware of it.

Any help will be so much appreciated.

Mina
 
Last edited:
Change you code in the report to

Code:
Private Sub Report_NoData(Cancel As Integer)
   
    MsgBox "There is no data to display"
    
    Cancel = True

End Sub

You should not need any additional code on the form at all, if the report has no data in it, it will display the message, once ok is pressed then close the report.

Hope this helps...
 
M8KWR said:
You should not need any additional code on the form at all, if the report has no data in it, it will display the message, once ok is pressed then close the report.
.
sadly that's not true
 
M8KWR,

I tried your code, still popped me into the 2501 msg.

Rich. . . any ideas?

Thanks to you both,
Mina
 
Shot in the dark, try removing the error handler from the Report and swap the msgbox and cancel statements around, the other option is to define 2501 as a constant


Option Compare Database
Option Explicit
Const ConErrRptCanceled = 2501



then it becomes
If ConErrRptCanceled Then
etc
 
I tried swapping the statements, didn't work.

Sorry to be obtuse, but I wasn't sure whether the other suggestion, with altered code, was to be shaped like the original.

I declared the constant in Gen Decs of the form, and then did this with the code:

Code:
On Error GoTo ErrorHandler

            DoCmd.OpenReport "rptEquipmentMalfunctionsCustom", acViewPreview

ExitHandler:
    Exit Sub

ErrorHandler:


    If ConErrRptCanceled Then
    Resume ExitHandler

    Else
        MsgBox Err.Description
        Resume ExitHandler

     End If

2501 Still came up. . . did I structure it wrong?

Thanks again,
Mina
 
Are Option Compare Database Option Explicit statements listed above the Const ConErrRptCanceled = 2501 statement? have you compiled and saved all modules?
 
Rich,

Here's the Gen Decs for that form:

Code:
Option Compare Database
Option Explicit
Const ConErrRptCanceled = 2501

I compiled the whole database after your suggestion, same results.
I believe this will drive me barmy.

Mina
 
Unless there's a different method in 2003, your db may be corrupt, try importing everything into a new blank db
 
I believe I will. I may not get to this until Monday, then I will post what happened.

Thank you so much for your guidance,
Mina
 
Solution using "resume next" I used.

The NoData cancel event from the first report did not allow me to run the second report because I typically code the "Resume Exit..." in the error handling section.
So I used a "Resume Next" to ensure the second report runs, even if the first has NoData.

Private Sub RunReport_Click()
On Error GoTo Err_RunReport

DoCmd.OpenReport "rptFirstReport",acViewPreview
DoCmd.OpenReport "rptSecondReport", acViewPreview

Exit_RunReport:
Exit Sub

Err_RunReport:
Select Case Err.Number
Case 2501 'There were no records, so close this report, run the next!
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.description,,"ERROR " & err.Source
End Select
Resume Exit_RunReport

End Sub
 

Users who are viewing this thread

Back
Top Bottom