Not really... that's essentially what error handling does.
a standard example:
Private Sub Button_Click()
On Error Goto Err_Handler
DoCmd.SetWarnings False
DoCmd.RunSQL "your query"
DoCmd.SetWarnings True
Exit_Proc:
DoCmd.SetWarnings True
Exit Sub
Err_Handler:
MsgBox "Error message here"
Resume Exit_Proc
End Sub
Or if you really wanted to, you could do an IF/THEN statement to handle the
error, but only if you have On Error Resume Next, otherwise as soon as an
error occurs it goes right to the error handler (on error resume next doesn't
have very many places in normal coding... this way is not recommended at all)
ex:
Private Sub Button_Click
On Error Resume Next
DoCmd.SetWarnings False
DoCmd.RunSQL "yourquery"
If Err.Number <> 0 Then DoCmd.SetWarnings True
'You would need this one anyway...
DoCmd.SetWarnings True
End Sub
The only way to check for errors using IF/THEN is to use On Error Resume
Next, which as you can see in this case you wouldn't have to check anyway...
you still need to turn on the warnings. The problem with On Error Resume
Next is, say you have a misspelled query name in the DoCmd.RunSQL statement,
there's no way you would know about it (until you noticed things going very
wrong with your data and had to track it back to this procedure... not fun).
So, really all your code should resemble the first example, with an error
handling procedure and and exit procedure for each sub/function.
As far as making sure the warnings are turned back on, that's no big deal
either. Assuming you have an exit procedure, just add that extra
DoCmd.SetWarnings into the exit procedure and you'll never have to worry
about it. Try an example using a nonexistent query name to see how it
handles it.
I made the comment about people not mentioning the dangers involved with not
resetting the warnings mainly because of how much of a royal pain it would be
for you if you didn't know about it. Consider, in the exit procedure of the
first example, leaving that DoCmd.Setwarnings line out, and mistyping the
query name... you would get no other warnings. Who knows how long it would
be until you realized it, and how much work it would take. A single
one-liner in the exit procedure makes that nasty possibility go away.
So if you don't use normal error handling and exit procedures, by all means,
start doing so, and just add in that one line in the exit. If you're not
aware, check out
www.mztools.com and download the addin for VBA. It wall
automatically add the exit and error handlers for you in any procedure.
Myself and hundreds of others swear by this program.
good luck!
--
Jack Leach
www.tristatemachine.com
"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)