Making th code more compacter

odrap

Registered User.
Local time
Today, 01:41
Joined
Dec 16, 2008
Messages
156
in my database program i use for practically every event the following errorcode: e.g.

Private Sub Form_Load()
On Error GoTo ErrorHandler

acbRestoreSize Me
Afsluiten:
On Error GoTo 0
Exit Sub
ErrorHandler:
Mededeling = foutbericht("Betaald_AfterUpdate", "OnbetldStatusKlnt", Err.Number)
Resume Afsluiten
End Sub

Can someone tell me how to change that code so that it can be called from every event , just by giving the name of the event as parameter, in order to circumvent the need to repeat this code in every event.
 
I don't think you can. On error goto is only applicable within the module where it is specified. I copy and paste my error trapping into every sub before I add any other code.
 
Looks like you're already using a global error handling routine.

One option you could look at is to just omit error handling from functions that are called deep in the stack:

Code:
Private Sub foo()

On Error Goto Oops

bar

Exit Sub
Oops:
HandleError("foo", Err.Number)

End Sub

Private Sub bar()

Dim i As Integer

i = 1/0

End Sub


I sometime like this variant which resemble a try/catch and IMHO is readable and allows me to ensure that I clean up all time without getting it all confused.

Code:
Private Sub foo()

Dim obj As Object

On Error Goto Catch:

Set obj = InvalidReference

Goto Finally
Catch:

Select Case Err.Number
   ....
   ....
End Select

Finally:

Set obj = Nothing

End Sub


In addition to Dennisk's method, you could use MZ-Tools which can automatically add error handling of your own specifications to the code, so it's a click of button instead of copying & pasting.
 

Users who are viewing this thread

Back
Top Bottom