Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question Title: Error Handling using VBA in Access

Status
Not open for further replies.

BearMist

Programmer
Jan 13, 2005
2
CA
I'm working in VBA, I have On Error GoTo (Variant) already being used in my functions / Subs, but I still have some Errors being caught by Access itself. I would like to catch these errors before Access does.

I would like to develop something similar to the 'try and catch' in VB.net, or if someone has something compartive to this.

Would anyone have a solution to this problem?

Thank You
 
Hi BearMist,

Not sure how much help it will be, but a brief explanation ..

You must explicitly set error handling in each routine where you want it to be enabled. You should also note that once an error has been trapped, further error trapping within the same routine is disabled until a Resume statement is executed. There is not really any alternative to this way of doing it.

If error trapping is not enabled in a routine, when an error occurs VBA will look up the calling chain until it finds a routine which does have enable error trapping and activate that error routine - the top of the calling tree is the application itself (Access).

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
something similar to the 'try and catch'
On Error Resume Next
... your tried statement ...
If Err Then
... your catch code ...
Err.Clear
End If
... and so on ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
not enough information for me, as I'm a new programmer and need more information to complete this task. Thank for you information, it helped me a lttle.
 
Hi BearMist,

As you asked for something equivalent to a VB.net construct it is reasonable to assume you know something about programming in general, and VB in particular. A slightly fuller explanation of error trapping is all I can really offer.

All errors in VBA code are handled using a simple error trap mechanism.

When an error occurs in a procedure, control passes immediately to the first inactive enabled error handling routine found by looking first in the current procedure and then in each procedure upwards in the calling chain. The very top of the calling chain is the Application (Excel or Access or whatever) and this always has such a routine (set in place by the application before starting to run your code).

When an error has been trapped, details of it can be found in the Err object.

An error handling routine is enabled (or disabled) by the execution of an On Error GoTo statement.
An error handling routine is activated by control transferring to it following an error (as stated above)
An error handling routine is deactivated (when active) by the execution of a Resume statement.

A special case is the On Error Resume Next statement. This is a shortcut which causes control to be passed to the next statement (activating an enabled trap) and simultaneously deactivating (though not disabling) the trap.

That's it in a nutshell. I would suggest you read up on the above constructs in Help and look at PHV's pseudocode - and then try coding something to meet your needs - an error trap in every routine maybe - depends on your requirements and the possible errors.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top