Error handling (1 Viewer)

kdt

Registered User.
Local time
Today, 20:17
Joined
Apr 5, 2006
Messages
48
Hi,

I am in the process of cleaning up my code as it has been a bit buggy to say the least :( . Currently I am using error handling for some of my subs, although I have read pretty much everywhere that it should be in all subs. As I have many subs per form, would it be wise to call error handling instead of putting
Code:
Private Sub DoSomething()
    ' Install the error handler.
    On Error GoTo UnexpectedError

    ' Do stuff.
        :

    ' Do not pass through into the error handler code.
    Exit Sub

UnexpectedError:
    ' Describe the error to the user.
    MsgBox "Unexpected error" & _
        Str$(Err.Number) & _
        " in subroutine DoSomething." & _
        vbCrLf & _
        Err.Description
    Exit Sub
End Sub
on every sub? Would be very interested to see what the responses are on this one.
 

Banana

split with a cherry atop.
Local time
Today, 12:17
Joined
Sep 1, 2005
Messages
6,318
If you search the forum, there is a public function for generic error handling. I use that all time, and even use it to create an automatic log of any errors to a table.
 

pono1

Registered User.
Local time
Today, 12:17
Joined
Jun 23, 2002
Messages
1,186
Food for thought: You don't necessarily need to put an error handler in every sub but as a general rule you should put an error handler in all "entry" subs. That is, if a procedure with an error handler calls another procedure without an error handler and the called procedure throws an error, it -- the error -- will bubble back up the stack and be handled by the calling procedure's error handler. Depending on what you're doing exactly, this can be a reasonable solution...

Very roughly....

button_Click with error handler --> calls --> GetNumber procedure : Error!

button_Click with error handler <-- error <-- GetNumber: Return error.

Regards,
Tim
 

kdt

Registered User.
Local time
Today, 20:17
Joined
Apr 5, 2006
Messages
48
If you search the forum, there is a public function for generic error handling. I use that all time, and even use it to create an automatic log of any errors to a table.

Hi Banana

Is this the error handling that you use?
[URL="http://www.access-programmers.co.uk/forums/showthread.php?t=94358&highlight=error+handling"[/URL]
How would I call this? Do you use

On error Call xx or a GoTo statement

sorry for the newb question.

Thanks
 

Banana

split with a cherry atop.
Local time
Today, 12:17
Joined
Sep 1, 2005
Messages
6,318
Kdt, yes that's the module I use all time. If you want to use that, don't forget to create a table named tblErrorLog with those fields:

tblErrorLog
ErrorLogID
ErrModule
ErrProcedure
ErrNumber
ErrDescription

to make the module work correctly.

You would put this in a module of its own. In the subs you call the function:

Code:
Private Sub Something

On Error Goto PROC_ERR

'Execute your code here

PROC_EXIT:
Exit Sub
PROC_ERR:
Call ShowError("'Name of the Module this sub is in", "'Name of this Sub or Function", Err.Number, Err.Description)
Goto PROC_EXIT

End Sub
 

kdt

Registered User.
Local time
Today, 20:17
Joined
Apr 5, 2006
Messages
48
Hi

Thanks a lot for this, it's exactly what I've been looking for. Just one problem...

I'm getting this "Expected variable or procedure, not module" on the Call ShowError line. Any ideas on what to do?

Thanks
 

Banana

split with a cherry atop.
Local time
Today, 12:17
Joined
Sep 1, 2005
Messages
6,318
I checked my code. It looks right.

Did you put the error handling function in its own module?
 

kdt

Registered User.
Local time
Today, 20:17
Joined
Apr 5, 2006
Messages
48
yep, created a new one for it and called it ShowError in modules. Does this seem right?
 

Banana

split with a cherry atop.
Local time
Today, 12:17
Joined
Sep 1, 2005
Messages
6,318
That may be why; Access could be confused it with module. Rename the module to something like modErrorHandling. You want Access to call the Function ShowError within the Module modErrorHandling, not to call a module named ShowError.

HTH.
 

kdt

Registered User.
Local time
Today, 20:17
Joined
Apr 5, 2006
Messages
48
Cheers, working now! Brilliant!
 

boblarson

Smeghead
Local time
Today, 12:17
Joined
Jan 12, 2001
Messages
32,059
Also, I like using MZ-Tools (free) and it allows you to enter the error handling code you want to add and then you can just be in a procedure and click the button and it will add the code for you. Makes it extremely easy to add error handling and it works with code already in the procedure too.
 

Users who are viewing this thread

Top Bottom