ACC2: How to Cancel a Report Which Contains No Records

ID: Q151587


The information in this article applies to:


SYMPTOMS

Moderate: Requires basic macro, coding, and interoperability skills.

Reports continue to display a page even when the recordset that the report is based on does not return data. When the recordset does not return data, controls based on the recordset can return errors to the page.


RESOLUTION

To avoid this behavior, test the report's underlying recordset to see if data is returned. If no data is returned, use the CancelEvent action to stop the report from processing.

NOTE: For more information about canceling a blank report using the new NoData event in Microsoft Access for Windows 95, search for "NoData event," using the Microsoft Access for Windows 95 Help Index.

The following method shows how to cancel printing a report when it is blank. A message box notifying the user that printing has been canceled is also displayed.

If the report is based on a parameter query, use the Query-by-Form technique to obtain the values for the parameter query before the report is run. The form and values will then be available as the DLookup function re- runs the query to determine if any records are available to be printed.

Call the following Sub procedure from the report's OnOpen event:


   Sub Report_Open (Cancel as Integer)
      Dim Msg as String
      If IsNull(DLookup("<AnyFieldInQuery>","<QueryName>")) Then
         Msg = "The report has no data. " & Chr(13) & Chr(10)
         Msg = Msg & "Check the source of data for the" & Chr(13) & Chr(10)
         Msg = Msg & "report to make sure you entered the" & Chr(13) & _
               Chr(10)
         Msg = Msg & "correct criteria (for example, a " & Chr(13) &  _
               Chr(10)
         Msg = Msg & "valid range of dates). " & Chr(13) & Chr(10)
         MsgBox Msg
         Cancel = True
      End If

   End Sub 


REFERENCES

For more information about the Query-by-Form technique, please see the following article in the Microsoft Knowledge Base:

Q95931 ACC: How to Use the Query-by-Form (QBF) Technique

Additional query words: null empty


Keywords          : kbusage RptEvent 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: April 20, 1999