Allen Brown's Error Handling in VBA (1 Viewer)

Laurad

Registered User.
Local time
Today, 11:05
Joined
Jan 16, 2011
Messages
68
I am trying to make use of the Error Handling code:

http://allenbrowne.com/ser-23a.html

I've created the tLogError table and I've copied the code into a Module.

What I am not sure about is how to call the function.. Can someone give me some example code as to how to use it?
Many thanks
Laura
 

rsf

Registered User.
Local time
Today, 03:05
Joined
Sep 18, 2009
Messages
15
The function gets called in the error trapping section of your own sub/function.

So (as taken from Allen Browne) you'll have something like this in all your own subs/functions (obviously 'SomeName' would be replaced with the name of your sub or function!):

##########

Sub|Function SomeName()
On Error GoTo Err_SomeName ' Initialize error handling.
.
.
<Your own sub or function code will be here>
.
.
Exit_SomeName: ' Label to resume after error.
Exit Sub|Function ' Exit before error handler.

'And this is the error trapping code that needs be in all your subs and functions
Err_SomeName: ' Label to jump to on error.
Select Case Err.Number
Case 9999 ' Whatever number you anticipate.
Resume Next ' Use this to just ignore the line.
Case 999
Resume Exit_SomeName ' Use this to give up on the proc.
Case Else ' Any unexpected error.
Call LogError(Err.Number, Err.Description, "SomeName()")
Resume Exit_SomeName
End Select
End Sub|Function

#############

That 'Call LogError' line is what's calling the LogError function and will write the error details to the tLogError table
 
Last edited:

Laurad

Registered User.
Local time
Today, 11:05
Joined
Jan 16, 2011
Messages
68
Thank you so much for such a swift and clear reply. I've now experimented and implemented it into my code and it works and I'm very pleased with the result. Thank you Allen Browne too.

The only thing I did not use because I'm not clever enough with MS Access is to anticipate the possible Error Codes.

eg:
Select Case Err.Number
Case 9999 ' Whatever number you anticipate.
Resume Next ' Use this to just ignore the line.


or this I did not fully understand.. is it necessary to add it?



Case 999
Resume Exit_SomeName ' Use this to give up on the proc.

Edited to add:
I don't understand the opetional parameters in Allen Browne's function:
vParameters
bShowUser

Thank you again, making progress now.

Laura
 
Last edited:

Laurad

Registered User.
Local time
Today, 11:05
Joined
Jan 16, 2011
Messages
68
One other thing, would it be wise and good practice to add this error logging to every procedure or just those where I might anticipate there could be an error? Most of the time I use the default coding that appears when a new button is added to a form, for example in the onClick event.
Thanks again
Laura
 

rsf

Registered User.
Local time
Today, 03:05
Joined
Sep 18, 2009
Messages
15
Those "Case 999" and "Case 9999" statements are just to give you examples of how you can tailor Access' behaviour in the event of an error occurring, you don't have to use them.
So if you wanted Access to ignore error code '1234' and then continue to execute your code, you would use
####
Case 1234
Resume Next
####
(whether it's wise to do this depends on what your code's doing and what the error is)

If you just want to log all errors then you can take out those "Call 999..." and "Call 9999..." statement blocks and just leave the "Call LogError..." statement in place.

As for knowing what error codes to anticipate... there's probably someone out there who knows the codes off by heart, but I'm not one of them! You'll most likely find out the codes of any errors you specifically want to trap if/when they occur as you're testing your code.
(If you're interested though, you can compile a list of the error codes using this: https://support.microsoft.com/en-us/kb/105666)

As far as I can see, the vParameters is just a placeholder that you can use to record anything extra that you might want, but it's not necessary, it's purely optional.

vShowUser looks like it can be used to suppress the error message that Access will display when an error occurs. The code displays this error message by default, I'd leave it like that.

Whether or not to use this code in every procedure probably comes down to personal preference... I think I would only use it in subs/functions that I have coded myself (to help with debugging my own code) rather than in code created by the Access wizards for command buttons etc.

But I'm not a professional programmer so someone wiser might advise differently...

Hope that helps.
 

Laurad

Registered User.
Local time
Today, 11:05
Joined
Jan 16, 2011
Messages
68
Yes, it all helps enormously, thank you so much for taking the time to explain. I am very grateful.
 

Users who are viewing this thread

Top Bottom