New to Error Handling. Any Advice?

SyntaxSocialist

Registered User.
Local time
Today, 09:06
Joined
Apr 18, 2013
Messages
109
Hey all. Just wrapping up my first access programming project and am integrating some error handling code. Never worked with it before. Found this and this very helpful in getting me started.

Question: I'm trying to sort out how to put multiple error handlers in one procedure. For example, I might want "On Error GoTo ErrHandler1" to encompass half of a procedure so that any error in that half will display a MsgBox and then Resume Next, but then want "On Error GoTo ErrHandler2" to encompass the other half and to display a different MsgBox before exiting the procedure. Do I need to tell the code to ignore the previous GoTo statement in order for it to recognize the next one?

From my internet searches I've gleaned that maybe all I need to do is something like the following. I'd love some feedback on whether I'm way off or on the right track. :)

Code:
Private Sub DoesStuff()

On Error GoTo ErrHandler1:
    'Code that might produce an error

On Error GoTo ErrHandler2:
    'Code that might produce a different error

    Exit Sub

ErrHandler1:
    'Code that displays a MsgBox with error information
    Resume Next

ErrHandler2:
    'Code that displays a different MsgBox with error information
    Exit Sub

End Sub

Any additional tips/tricks/resources you'd like to point me towards would be appreciated as always
 
First off, read this Wiki page...

Robust VBA Error Handler
http://www.access-programmers.co.uk/wiki/index.php/Robust_VBA_Error_Handler

I'm trying to sort out how to put multiple error handlers in one procedure.

Yes, you will need separate error handler labels if you want to customize the error handling in various sections of a Function / Subroutine / etc...

If you simply want to customize the text of the error message indicating where specifically the code blew up, then you could still use the Wiki suggested link, and at specific points in the code if your programming logic realizes that something went "business logic wrong" but not actually a crash to the code, then you could call the error handler passing in a customized text message of where the error was detected, and bomb out of the function. Example:

Code:
  'Open the XMLSchemaVersion
  Set objXMLNodeXMLSchemaVersion = objXMLDOMDoc.selectSingleNode("//FASMessage/XMLSchemaVersion")
  If objXMLNodeXMLSchemaVersion Is Nothing Then
    [B]Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: LoadXML(), Failed: objXMLDOMDoc.selectSingleNode('//FASMessage/XMLSchemaVersion') RC=1", , objXMLDOMDoc)[/B]
    GoTo Exit_LoadXML
  End If

  'Save the XMLSchemaVersion
  Me.XMLSchemaVersion = objXMLNodeXMLSchemaVersion.Text

  'Test the XMLSchemaVersion
  If strMasterXMLSchemaVersion <> Me.XMLSchemaVersion Then
    [B]Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: LoadXML(), Failed: strMasterXMLSchemaVersion <> Me.XMLSchemaVersion, Values: strMasterXMLSchemaVersion=" & strMasterXMLSchemaVersion & " Me.XMLSchemaVersion=" & Me.XMLSchemaVersion & " RC=1", , objXMLDOMDoc)[/B]
    GoTo Exit_LoadXML
  End If

  'Open the FASRule
  Set objXMLNodeFASRule = objXMLDOMDoc.selectSingleNode("//FASMessage/FASRule")
  If objXMLNodeFASRule Is Nothing Then
    [B]Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: LoadXML(), Failed: objXMLDOMDoc.selectSingleNode('//FASMessage/FASRule') RC=1", , objXMLDOMDoc)[/B]
    GoTo Exit_LoadXML
  End If

  'Save the FASRule
  Me.fasrule = objXMLNodeFASRule.Text

  'Open the FASRuleMessage
  Set objXMLNodeFASRuleMessage = objXMLDOMDoc.selectSingleNode("//FASMessage/FASRuleMessage")
  If objXMLNodeFASRuleMessage Is Nothing Then
    [B]Call errorhandler_MsgBox("Class: " & TypeName(Me) & ", Subroutine: LoadXML(), Failed: objXMLDOMDoc.selectSingleNode('//FASMessage/FASRuleMessage') RC=1", , objXMLDOMDoc)[/B]
    GoTo Exit_LoadXML
  End If

  'Save the FASRuleMessage as XML
  Me.fasrulemessage = objXMLNodeFASRuleMessage
 
Thanks!

So do I need to tell the code to ignore the previous GoTo statement in order for it to recognize the next one? (The link you provided was broken)
 
Download the free utility MZTools for VBA.

It will create basic error handling for you, and much more.
It's a good place to start.

Good luck.
 
COOL! Any advice re: resources for nested error handling? I've hit a hurdle on that now, too... :S
 
So do I need to tell the code to ignore the previous GoTo statement in order for it to recognize the next one?

If you issue a new On Error GoTo xyz... LOC then that wipes out the prior setting in a given Function / Subroutine / ....

(The link you provided was broken)

I will report that problem to Jon. Thanks for the FYI.
 

Users who are viewing this thread

Back
Top Bottom