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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When report has no data

Status
Not open for further replies.

bdm1

Programmer
Feb 4, 2002
75
US
I hsve a form which allows for printing various reports. All the coding for the report is attached to the click event of a command button on this form after the choice has been made. If there is no data to report, how can I avoid getting a run time error # 2501 message that says the open report action was canceled? I have inserted a message to the OnNoData event of the report but am still getting that message. I suspect that I have to handle this from the command button but am not sure of the syntax or where exactly to insert it. Appreciate any suggestions....thanks
 
Hi,

Use this code to avoid the error

Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for this report. Canceling report..."
Cancel = -1
End Sub

This is from Microsoft templates download "Expenses"

For more details check this link

Regards
Zameer
 
Appreciate your suggestion but it required a bit more steps. Finally figured it out at about 2am......Thanks again
 
Insert this code behind the button you use to open the report. (cmdButton refers to the name of your button)

Private Sub cmdButton_Click()
On Error GoTo ErrorHandler

DoCmd.OpenReport "rptName", acViewPreview

ExitHandler:
Exit Sub

ErrorHandler:

' This traps the Output to Error message
If Err = 2501 Then
Resume ExitHandler

Else
MsgBox Err.Description
Resume ExitHandler

End If

End Sub

Insert this code in the On NoData event procedure of the report:

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

MsgBox "There are currently no records", vbOKOnly, _
"Title of Message Box"

Cancel = True

ExitHandler:
Exit Sub

ErrorHandler:
MsgBox Err.Description
Resume ExitHandler

End Sub
 
You can also put your coding in some "SQL"-String and use the DCount() function to determine whether the report will have records before you open/print it:
Code:
Dim sSQl as String
sSQL=&quot;SELECT ... &quot; <-- your report source SQL

If DCount(&quot;[ID]&quot;, sSQL)=0 then
 msgbox &quot;There is no data to print&quot;
 goto myEnd
End If
DoCmd.OpenReport ....
...
myEnd:
End Sub
Then you just skip the opening procedure for empty reports.

Regards,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top