QuickStart: Automatic line numbering

Overview

It is often beneficial to have line-numbering in VB applications so that you can identify the exact line number where an exception occurs.

vbWatchdog removes the need to manually add any line numbering to your sourcecode by providing built-in detection of the line number, obtained from the compiled debug information already stored in your applications.

How line numbering normally works in VBA and VB6

Normally, to add line numbering to VB code, we have to do something like this:

Public Sub A()
1   On Error GoTo ErrorHandler
2
3       Debug.Print 1 / 0
4
5       Exit Sub
ErrorHandler:
    MsgBox "Error line number: " & Erl
End Sub

As you can see, trying to add line numbers like this makes writing code more difficult to write and maintain.

Also, adding explicit line numbers like this does bloat the application - simply because of all the extra data that is being stored for every single line of source code.

Fortunately we've come up with a much better line numbering system...

The vbWatchdog automatic line numbering system

Wouldn't it be great if we could identify the line number from the compiled code, without actually having to write any line numbers?

Well that's exactly how the line numbering system in vbWatchdog works!  When an error occurs, internally we parse through the PCode (compiled code) and can very accurately determine the line number that caused the error.

In your global error handler, use ErrEx.SourceLineNumber to get the line number (or ErrEx.CallStack.LineNumber when iterating through the call stack)

This feature works equally well for both fully compiled projects (like .MDE Access database applications) as well as normal semi-compiled VBA projects (like .MDB Access database applications).

Tip: The line number that is referred to here does not count any white-space in the source code, nor does it count any lines that don’t have any code (e.g. Dim statements, comment lines etc).

Next:  Easily determining the line numbers from within the VBE code pane...

The VBE Add-in to easily reveal line numbers in the VBE codepane

vbWatchdog comes complete with a COM add-in that provides you with a new menu bar button in the VB IDE which enables you to very clearly see the line numbers in your source code.

Toggle Line Number button

By pressing the ‘Toggle SourceLineNumbers’ button, you will see the following extra bar added to your codepane:

Line number side bar

Programmatically identifying the line of source code at the error line number

You might wonder how you can convert the source line number indicated by ErrEx.SourceLineNumber into the actually line of code (e.g. for displaying the code in your error dialog).

Dialog example of line source code


(In the above example, #10 is the SourceLineNumber and ‘Debug.Print 1/0’ is the source code at line 10 in the subroutine called TestLineNumbering)

For this purpose, we have provided ErrEx.SourceLineCode and ErrEx.CallStack.LineCode - these properties simply lookup the procedure source code and return the line of code as a string expression (as per the dialog example above).

For fully compiled projects (such as Access MDE / ACCDE applications), ErrEx.SourceLineCode will return a blank string since the source code is not available. In this scenario, use ErrEx.SourceLineNumber instead to identify the line of error in your source code.