The Error Class Revisited

In the comments to Error Handling Via an Error Class, Peter found that the problem with this method is the absence of the Stop and Resume in the error handler than let you debug at the line that caused the error. Yeah, that stinks. Then Jase got me thinking that I just wouldn’t create the class in debug mode. Well, that wasn’t quite right. What needed to happen was that the error handler should not be set in debug mode. Here’s a rewrite of the entry point procedure.

Sub EntryPoint()
   
    Dim clsError As CError
   
    gbDebugMode = False
                   
    If Not gbDebugMode Then On Error GoTo ErrHandler
   
    Set clsError = New CError: clsError.SetLoc "Module1", "EntryPoint"
   
    SubProc1
     
ErrExit:
    Exit Sub
   
ErrHandler:
    Set clsError = Nothing
    MsgBox Err.Description
    Resume ErrExit
   
End Sub

When gbDebugMode is False, the error handler is set and it works as described in the original post. That is, the user gets a message box and the code exits gracefully. When gbDebugMode is True, the error handler is not set. It’s like you don’t have an error handler at all – because you don’t. When in debug mode, you get kicked to the line that caused the error.

Is that that last hurdle?

7 Comments

  1. Alex Godofsky says:

    It seems like Microsoft could save everyone a whole lot of effort by updating VBA to C# or VB.NET.

  2. Peter Albert says:

    I like the idea! Will implement in my next project and get back to you with any feedback! :-)

  3. Jase says:

    SIMPLE! SIMPLE! and Elegant. I love this and think you nailed it this time.

  4. David says:

    Note, you can achieve a similar result with Tools | Options in the Visual Basic editor. Just set the option to break on all errors. I frequently do this to defeat my error trapping when I want to look for a bug.

  5. Lars says:

    Hi together,

    has anybody tried this under Windows 8 and Office 2013?! I really like this ErrorClass and it worked fine for all my vba-code.
    My problem is that the same file (absolutely no modifications) handles the errors under windows 7 (at home) and does not handle errors und windows 8 (at my company). When the code jumps back from the main-procedure into the error-class, the err-object is always NOTHING. So there is no error-handling anymore with this tool :(

    Has anybody experience with this… or any ideas?!
    Thanks for any help.

    Greets,
    Lars

  6. Dick Kusleika says:

    That’s strange Lars. There shouldn’t be any difference between Windows or Office versions. Check your VBE Properties, specifically Tools – Options – General – Error Trapping and make sure they’re the same in both versions.

  7. Lars says:

    Hello Dick,

    thanks for your quick answer. The only thing I did not try was to restart the computer. This morning everything was fine with the error class again.

    Thanks a los and have a very nice weekend.

    Greets,
    Lars

Posting code or formulas in your comment? Use <code> tags!

  • <code lang="vb">Block of code goes here</code>
  • <code lang="vb" inline="true">Inline code goes here</code>
  • <code>Formula goes here</code>

Leave a Reply

Here's how to update your reports of company and nearly any web data: