Blank Reports

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form where users can select to view the report based on criteria in
the form. At times, of course, there is no item that meets the criteria and
when the report is previewed, it shows a blank form with the various headers.
How do I go about putting a dialog box that will pop to say something like
"Sorry no records were found" without opening the report in preview? Thanks.
 
Cancel the NoData event of the report.

Access triggers the NoData event of the report when it loads with no records
in its Recordset. By cancelling this event, the report does not
preview/print. The event procedure is just:
Private Sub Report_NoData(Cancel As Integer)
Cancel = True
MsgBox "Sorry no records were found"
End Sub

You will need to use error handling in the code that opens the report. That
code will be informed that the OpenReport failed by error 2501. You will
therefore need to trap and discard this error. This kind of thing:
Private Sub cmdPrint_Click()
On Error Goto Err_Handler
Dim strWhere As String
strWhere = "ID = " & [ID]
DoCmd.OpenReport "Report1", acViewPreview, , strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
Resume Exit_Handler
End Sub

If error handling is a new concept, see:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html
 
Back
Top