QuickStart: Automatic line numbering



 PLEASE NOTE: SimplyVBA and SimplyVB6 have been superseded by vbWatchdog.
This page exists for archival purposes only.  
Upgrade licences of vbWatchdog are available at a discounted rate.

Why do we need line numbering?

There are two typical scenarios that benefit from line numbering:

  1. The developer distributes compiled application files (e.g. Access MDE/ACCDE compiled files or VB6 EXE/DLL applications) to end users where the source code is unavailable. When errors occur, line numbers are logged so that the developer can ascertain the exact line of source code that produced the error by looking up the line number in the source code.
  2. The developer distributes Access MDB/ACCDB files to end users where source code is available, but they don't want to encourage users to debug through the code if an error occurs. When errors occur, line numbers are logged so the developer can ascertain the exact line of source code that produced the error from the MDB / ACCDB at their end.

How line numbering normally works in VBA

Normally, to add line numbering to VB code, we have to 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...

Our Global Error Handler 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 our Global Error Handler 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, VB6 exe applications etc) 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

The Global Error Handler comes complete with a COM add-in that provides you with a new menu bar button in the VBA/VB6 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 A)

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.