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