Report OnNoData event error

S

Seth

In the report OnNoData event I have the following code to display a message
if the report contains no data.

Private Sub Report_NoData(Cancel As Integer)
MsgBox "Your query did not return any data."
Cancel = True
End Sub

It works when I open the report manually but when I open the report with
code behind a form an error is generated.

The code behind my form is:
DoCmd.OpenReport "rptTestRprtsByModel", acViewPreview


The error is:
Error No: 2501; Description: The OpenRepoer action was canceled.
You used a method of the DoCmd object to carry out an action in Visual
basic, but then clicked cancel in a dialog box. Foe example, you used the
Close metnod to close a changed form, then clicked Cancel in the dialog box
that askes if you eant to save the changes you made to the form

How do I prevent this error?

Cheers,
Seth
 
N

Nikos Yannacopoulos

Seth,

If the report's recordsource is a saved query, then it's as easy as:

If DCount("*", "The Saved Query Name Here") = 0 Then
MsgBox "Your query did not return any data."
Exit Sub
End If
DoCmd.OpenReport "rptTestRprtsByModel", acViewPreview

so the attempt to open the report will not be made at all if there is no
data.

HTH,
Nikos
 
S

SA

Seth:

Actually you can handle the error more easily than Nikos suggests. There's
no reason to do a DLookUp on the reports query, that will simply make the
process longer. You can solve the problem by adding a little error handler
to your function that calls the report on the form as shown below.
--
Steve Arbaugh
ACG Soft
http://ourworld.compuserve.com/homepages/attac-cg

-----------begin code----------
Private Sub cmdPreviewReport ()
On error goto ErrHandler

DoCmd.OpenReport "rptTestRprtsByModel", acViewPreview

ExitProc:
Exit Sub
ErrHandler:
If Err.Number = 2501 Then
'Ignore the error
Resume ExitProc
Else
MsgBox "Error: " & Err.Number & " " & Err.Description, 16, "Report
Print"
Resume ExitProc
End If
End sub
-------------end code----------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top