NoData

  • Thread starter Thread starter Derek Brown
  • Start date Start date
D

Derek Brown

Hi All

How do you test to see if a report HasData from a "Open Report" button on a
main form.

I have used the Has data property of the report but it doe not stop the rest
of the buttons code from running.

I need something like

If Report.HasData = False then
Cancel=True
Else
Do something
End if

Does that make any sense?
 
If the report is based on a query, you could perhapsdo

checkrec = dcount ("[fieldname that always has data]", "Report Query")
if checkrec > 0 then
do something (do report etc.)
else
msgbox ("There is no information that will show in this report")
endif.

Ron
 
Derek Brown said:
Hi All

How do you test to see if a report HasData from a "Open Report" button on a
main form.

I have used the Has data property of the report but it doe not stop the rest
of the buttons code from running.

I need something like

If Report.HasData = False then
Cancel=True
Else
Do something
End if

Does that make any sense?

Derek,

To stop the rest of the code from running (actually skip the rext of the
code), add "Exit Sub" after "Cancel = TRUE".


If Report.HasData = False then
Cancel=True
' No data, so skip the rest of the code
Exit Sub ' <<<<<<<<<
Else
Do something
End if

HTH
 
Private Sub Command0_Click()

On Error GoTo ErrorHandler
DoCmd.OpenReport "rptTest3", acViewPreview, , "AccidentID = 0"
MsgBox "If Cancel is set to True in Report's NoData event procedure, " &
_
"you'll never see this message, " & _
"because execution will jump to the error handler."

ExitProcedure:
MsgBox "See, I told you so!"
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
MsgBox "Didn't believe me, huh?"
Else
MsgBox "Something unexpected happened"
End If
Resume ExitProcedure

End Sub
 
Derek Brown said:
Hi All

How do you test to see if a report HasData from a "Open Report"
button on a main form.

I have used the Has data property of the report but it doe not stop
the rest of the buttons code from running.

I need something like

If Report.HasData = False then
Cancel=True
Else
Do something
End if

Does that make any sense?

I'm not completely sure what you're trying to do. One possibility would
be to check the report's HasData property in the report's Open event,
and cancel the event (and thus the opening of the report) if there's no
data:

'----- start of code example #1 -----
Private Sub Report_Open(Cancel As Integer)

If Me.HasData = False Then
MsgBox "There's no data to report."
Cancel = True
End If

End Sub

'----- end of code example #1 -----

Cancelling the report will raise error 2501 ("action cancelled") in the
code that opened the report, so that code can trap that error and take
an early exit:

'----- start of code example #2 -----
Private Sub cmdReport_Click()

On Error GoTo Err_Handler

DoCmd.OpenReport "YourReport", acViewPreview

' ... other code to be executed if the report wasn't cancelled ...

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
' ignore this -- action was cancelled
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
'----- end of code example #2 -----

Maybe that's not what you're looking for, but it's one approach.
 
Thanks everyone

Hi Dirk.
All I'm trying to do is cancel a report if the report would be empty. The
report normally is opened by clicking a button on a form but the button also
runs other code like hiding a Pop up form until the report has finished.

I can cancel the report by the reports has data proerty but this does not
stop the button from going on to hide the pop up. which I don't want if open
report is cancelled. Any Ideas?. I can't imagine that I cannot have a button
which simply asks "has this bloody form got data or not"

Thanks Dirk
 
Look at the example I posted, Derek. It was a bit flippant, I hope that
didn't put you off. There's a slightly less flippant version below! :-) If
your code that hides the pop-up form goes where I have my first MsgBox call,
it will not be executed if the Cancel argument is set to True in the NoData
event procedure of the report.

In the report module ...

Option Compare Database
Option Explicit

Private Sub Report_NoData(Cancel As Integer)
Cancel = True
End Sub

In the form module ...

Option Compare Database
Option Explicit

Private Sub Command0_Click()

On Error GoTo ErrorHandler
DoCmd.OpenReport "rptTest3", acViewPreview, , "AccidentID = 0"
MsgBox "If Cancel is set to True in Report's NoData event procedure, " &
_
"you'll never see this message, " & _
"because execution will jump to the error handler."

ExitProcedure:
MsgBox "This code is always executed."
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
MsgBox "Put any code you want to run only if the report is cancelled
here."
Else
MsgBox "Put code to handle unanticipated errors here."
End If
Resume ExitProcedure

End Sub
 
Brendan Reynolds said:
Look at the example I posted, Derek. It was a bit flippant, I hope
that didn't put you off. There's a slightly less flippant version
below! :-) If your code that hides the pop-up form goes where I have
my first MsgBox call, it will not be executed if the Cancel argument
is set to True in the NoData event procedure of the report.

In the report module ...

Option Compare Database
Option Explicit

Private Sub Report_NoData(Cancel As Integer)
Cancel = True
End Sub

In the form module ...

Option Compare Database
Option Explicit

Private Sub Command0_Click()

On Error GoTo ErrorHandler
DoCmd.OpenReport "rptTest3", acViewPreview, , "AccidentID = 0"
MsgBox "If Cancel is set to True in Report's NoData event
procedure, " & _
"you'll never see this message, " & _
"because execution will jump to the error handler."

ExitProcedure:
MsgBox "This code is always executed."
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
MsgBox "Put any code you want to run only if the report is
cancelled here."
Else
MsgBox "Put code to handle unanticipated errors here."
End If
Resume ExitProcedure

End Sub

Brandan -

The only reason I didn't suggest the NoData event was that, for a test
report I tried, the event seemed to be firing twice. That struck me as
odd, but I didn't have time to investigate in detail. I don't think it
necessarily matters if all you do in the NoData event procedure is
cancel the report, but if you display a message as well, you may see
that message twice. Have you seen this?
 
I haven't seen that, Dirk. One of our most widely-deployed apps has code in
the NoData event of each report that displays a message box informing the
user that no data matches the current criteria, and I haven't had any
reports of the message being displayed twice. Of course, that doesn't mean
that it has never happened - it wouldn't prevent use of the application, so
it is possible that some users may have encountered it and just not bothered
to call us about it.
 
Hi Dirk

One of the problems with being self taught and learning databases purely on
a problem solving basis is that I have no idea how to use Error handling. I
believe that problems should be solved not handled. That could be extremely
naive, but as I don't work with anyone else and have nothing to compare my
work with, I know no better and consequently have never had that particular
error but I certainly have seen stranger things.

In the code below is "AccidentID=0" something from an old example or is it a
command for VB?
I would like to try this solution if you could clarrify that point

Thanks Dirk
 
Hi Dirk

Just got my brain working again. Please ignor the last question AccidentID
is the MsbBox Title I guess.

Thanks again. Will give it a try!
 
Derek Brown said:
Hi Dirk

One of the problems with being self taught and learning databases
purely on a problem solving basis is that I have no idea how to use
Error handling. I believe that problems should be solved not handled.
That could be extremely naive, but as I don't work with anyone else
and have nothing to compare my work with, I know no better and
consequently have never had that particular error but I certainly
have seen stranger things.

There are different views on this. Here's my take: if a particular
error is (relatively) likely to occur and can easily be checked for it
happens, I use a preliminary check to avoid raising it. If the error is
foreseeable but less likely to occur, or if it is expensive to check for
in advance, I let error-handling take care of it, but may have a
specific section in my error-handling code for it. Obviously, I still
need general-purpose error-handling code for unforeseen errors. And
then there are some errors that are either can't be easily checked for
in advance, or else it's unnecessarily expensive to do so. For these I
will definitely use error-handling.

Really, it's not so much *error*-handling as *exception*-handling -- the
mechanism exists to handle exceptional conditions, whether they are
actual errors or not. An exception is anything that the developer views
as an exception to the normal flow of the code.

In the case you're dealing with, opening and displaying the report is
the normal sequence of events, it seems to me. The fact that the report
might contain no data is an exception, which we don't expect to happen
most of the time. We *could* open a separate query on the report's
recordsource to check in advance whether the report will contain data or
not. If we do that, though, we're going to incur the expense of that
query every time we open the report, even though most of the time the
report will have data and the extra check will have been unnecessary.
Since the report has a built-in mechanism for detecting the "no data"
condition --actually two, as you can see by comparing Brendan's
suggestion with my own -- it makes sense to use that mechanism and just
cancel the report if it would be empty. That way, the query only gets
run once.

The only hitch is that cancelling an action (such as "OpenReport") that
was initiated in code generates an "error" -- really an exception, not
an error, from my point of view -- and therefore our code has to handle
that error gracefully. Adding an error-handler that will ignore that
"error" (2501 -- action cancelled) is the simple solution.
In the code below is "AccidentID=0" something from an old example or
is it a command for VB?
I would like to try this solution if you could clarrify that point

You're talking about this example code that Brendan posted.

In that code, "AccidentID = 0" is a "where-condition" argument passed to
the OpenReport method to filter the report to show only records with a
specific AccidentID; in this case, AccidentID 0. That's just an
example. If you were going to show all records on the report, you'd use
just this line to open it in print-preview mode:

DoCmd.OpenReport "rptTest3", acViewPreview
 
'AccidentID = 0' is a condition I used to test the code before posting it.
None of the records in my test data have an AccidentID field equal to zero,
so passing the condition 'AccidentID = 0' in the WhereCondition argument of
the OpenReport method ensured that the report would return no data, thereby
causing the NoData event to fire.
--
Brendan Reynolds
Access MVP


Derek Brown said:
Hi Dirk

Just got my brain working again. Please ignor the last question AccidentID
is the MsbBox Title I guess.
<snip>
 
Back
Top