View Full Version : How can I make my Report Print Only when...


DaniBoy
11-27-2001, 10:23 AM
Hello, I have some reports that I need printed only when there is data for them. Right now when I print a report, it prints the reports labels with no data in it. How could I fix this problem so that I will only print when there is data for that report.

Thanks
Daniel

Rich
11-27-2001, 11:19 AM
Use the No Data event of the report to display a message box and cancel the event
Private Sub Report_NoData(Cancel As Integer)
MsgBox "There is no data for the period you have selected", vbOKOnly

Cancel = True

End Sub

DaniBoy
11-27-2001, 11:54 AM
ok, i did it with the nodata event. now am actualy opening the report from a code on a form button, the code I used for this was the next one:

Private Sub TestNoData_Click()
On Error Resume Next
DoCmd.OpenReport "SomeReport", acViewPreview
If Err = 2501 Then Err.Clear
End Sub

But I still get the empty Data Report, what am I doing wrong? How could I send this Database to someone so they could see what am trying to do?!!
Thanks

Harry
11-28-2001, 02:03 AM
This code ought to work!!

Dim MyRS as Recordset
Dim MyRecs as single
set MyRS = Currentdb.OpenRecordset("Table/Query feeding report")
MyRecs = MyRS.RecordCount
If MyRecs = 0 then
else
DoCmd.OpenReport "SomeReport", acViewPreview
end if
MyRS.Close

HTH

Rich
11-28-2001, 06:21 AM
I suspect your error handling is preventing the no data event try this code
Const ConErrRptCanceled = 2501
Private Sub Preview_Click()
' Preview report.
Dim strDocName As String
On Error GoTo Err_Preview_Click


strDocName = "YourRpt"
DoCmd.OpenReport strDocName, acViewPreview


Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
If Err = ConErrRptCanceled Then
Resume Exit_Preview_Click
Else
MsgBox Err.Description
Resume Exit_Preview_Click
End If