On NoData Event

jmeek

Registered User.
Local time
Today, 05:16
Joined
Aug 16, 2005
Messages
38
I have the following code on the NoData Event
in a report.

Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data to Display"
Cancel = True
End Sub

If in fact no data is found for the report I then
get the following error message

"Run time error 2501 the OpenReport
action was cancelled"

I need to get rid of the error message. Why does
it appear anyway ? The idea of the Nodata event
should have sorted it out.

Can anybody help please

Many thanks
 
run tim error 2501

Hi Rich

I did seek and didn't find and still didn't find.

This event is the Nodata event not the OpenReport
event which however pops when the NoData event
triggers. Here is the full code once more.

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo Report_NoData_Error
MsgBox "No data to Display"
Cancel = True

Report_NoData_Exit:
Exit Sub

Report_NoData_Error:
If Err.Number = 2501 Then 'the report was cancelled
Exit Sub
Else
MsgBox Err.Number & " - " & Err.Description
Resume Report_NoData_Exit
End If


End Sub

As you can see, even inserting your code or the link you
gave me, it still brings up the error.
 
The error trap belongs on the form button that opens the report, not the report
 
run tim error 2501

Hi Rich

Thanks for your guidance.
All is working well now.
 
Hello Rich,

One of my report preview command btn has following code:

Private Sub PrnCashSalesInv_Click()
On Error GoTo Err_PrnCashSalesInv_Click

Dim stDocName As String

stDocName = "QR_CashSalesInvoice"
DoCmd.OpenReport stDocName, acViewPreview

Exit_PrnCashSalesInv_Click:
Exit Sub

Err_PrnCashSalesInv_Click:
MsgBox Err.Description
Resume Exit_PrnCashSalesInv_Click

End Sub

Incase I put following code in NoData events of the report, it gives error 2501

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo Report_NoData_Error
MsgBox "No data to Display"
Cancel = True

Report_NoData_Exit:
Exit Sub

Report_NoData_Error:
If Err.Number = 2501 Then 'the report was cancelled
Exit Sub
Else
MsgBox Err.Number & " - " & Err.Description
Resume Report_NoData_Exit
End If

Both event code (Form & Report) have no error if compiled separately. But if clicked the cmd btn from form when no data in report, the error occurs on line "DoCmd.OpenReport stDocName, acViewPreview" of the form code.

One more thing, when I run report directly, it runs smoothly. Pls advice.

Regards,

Ashfaque
 
Your error handling belongs on the form
Private Sub PrnCashSalesInv_Click()
On Error GoTo Err_PrnCashSalesInv_Click

Dim stDocName As String

stDocName = "QR_CashSalesInvoice"
DoCmd.OpenReport stDocName, acViewPreview

Exit_PrnCashSalesInv_Click:
Exit Sub

Err_PrnCashSalesInv_Click:

If Err = 2501 Then
Resume PrnCashSalesInv_Click:
Else
MsgBox Err.Description
Resume Exit_PrnCashSalesInv_Click:

End If

End Sub
 
Hi again,

Thanks for the immediate response.

Yes, the belongs to form button.

I think the code line should be ;

Resume Exit_PrnCashSalesInv_Click:

instead of Resume PrnCashSalesInv_Click:

But after correction also it showing invoice when there is no data.

I don't know how to solve it.

Regards,

Ashfaque
 
Dunno if this'll work in VBA, would work in normal VB though so give it a go,
Code:
Private Sub Report_NoData(Cancel As Integer)
MsgBox "No data to Display"
unload me
End Sub
 

Users who are viewing this thread

Back
Top Bottom