ehandle for every function sub?

scratch

Registered User.
Local time
Today, 15:58
Joined
May 10, 2005
Messages
98
I've read in various sources that every sub/function including built in ones like onClick() should have error handling. When I look at various examples of their code, I noticed that there isn't error handling in every procedure. Perhaps it's for the sake of readablility but what's the verdict? In EVERY sub/function or not?

scratch
 
Yes! Something as simple as this is all that is needed...

Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
    
    'your code here
    
Exit_Form_Open:
    Exit Sub

Err_Form_Open:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Form_Open
    
End Sub
 
What I do:
Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
    
    'your code here
    
Exit_Form_Open:
    Exit Sub

Err_Form_Open:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Form_Open
    [b]Resume[/b]
End Sub

Notice the extra resume (Another tip from WROX). When you experience the error press ctrl-break to go to your VBA. Then push F8 and you'll see what line the error code was just at.

It's also good to create an error log and print the error there. That way you can see what errors the users are encountering and fix them before they cause too much of a problem.
 
Although the programmer should encode their application so that the user can not access the VB editor and the db window if their is a runtime error. ;)

What is WROX?
 
ghudson said:
Although the programmer should encode their application so that the user can not access the VB editor and the db window if their is a runtime error.
-Password Protect-
 
error handler

I make it a general practice to include an error handler in all modules for vba....and error can occur which could cause the module to go into error mode..and then cause the programe to crash. I usually do the following as a 'shell' for all modules before putting in additional code.

Private Sub Name_of_module()

On error goto Error_handler:

Program Code goes here


Exit Sub

Error_handler:
Code to handle error / usually a Msg Box

End Sub
 
How is that any different then what was posted. You advised using the Module name instead of putting an error handler for each function. I don't suggest doing so.

As stated earlier, put in a handler for each procedure (sub/function)
 
Last edited:
I never include error handling when I post code samples unless that is what is being explained but my procedures all contain error handling.
 

Users who are viewing this thread

Back
Top Bottom