QuickStart: The Variables Inspector

What is the Variables Inspector?

The VariablesInspector object gives you the ability to programmatically inspect the values of all local variables in any or all of the procedures in the VB callstack.

When handling an exception in a global error handler, use the object returned by the ErrEx.Callstack.VariablesInspector property, and if you want to inspect the variables outside of a global error handler, then use the object returned by ErrEx.LiveCallstack.VariablesInspector instead.

Requirements for this feature

Works with all editions of VBA6 and VBA7, even if running a compiled MDE/ACCDE or when using the Access runtime.

Also available in VB6 with the ULTIMATE EDITION, but only when debugging in the IDE. The VariablesInspector is unavailable from a fully compiled VB6 project.

Displaying variable contents in the error dialogs

The error dialogs provided by vbWatchdog have built in support for displaying the Variable contents:

Please review the Customizing the Error Dialog document and also the Sample.MDB for further information.  The remainder of this document focuses on programmatically accessing the variables.

How to use the Variables Inspector object (simple)

In it's simplest form, you can call the VariablesInspector.DumpAll method which returns a String value containing a simple dump of all variable names, data types and current values:

Public Sub MyGlobalErrorHandler()

    MsgBox ErrEx.VariablesInspector.DumpAll

End Sub

This would produce something like this:

You can also traverse the call stack and for each procedure in the stack, dump all variables as a string:

Public Sub MyGlobalErrorHandler()

    Dim strDump As String
        ' Header text - module name + procedure name
        strDump = strDump & " >> " & ErrEx.CallStack.ModuleName & "." & ErrEx.CallStack.ProcedureName & vbCrLf & vbCrLf

        ' Dump the variable details from this procedure...
        strDump = strDump & ErrEx.CallStack.VariablesInspector.DumpAll & vbCrLf
    Loop While ErrEx.CallStack.NextLevel
    MsgBox strDump
End Sub

Which might produce:

How to use the Variables Inspector object (advanced)

Taking this a step further - perhaps you want to customize the "dump" into your own format. This is how:

Start by calling the .FirstVar method which ensures we've got a reference to the first variable. Next, loop through the variables using the .NextVar mthod but checking first that we haven't reached the end of the variables list using the .IsEnd method.

Whilst iterating through the variables, use the following read-only properties to identify the variable:

.NameString expression of the variable name.
.TypeDescstring expression of the variable data type (e.g. 'DAO.Database', 'Long', 'String' etc)
.ValueRead-only, variant copy of the variable value (no matter what data type, all values are coerced into this Variant property)
.ValueDescstring expression of the value. String data types have the double-quotation marks added, 'Object' types are either 'Nothing' or '{Object}' etc. It is advised to use .ValueDesc rather than .Value under normal circumstances.
.Scopeenumeration to determine what type of variable this is - One of ParameterVariable, LocalVariable, StaticVariable or ModuleVariable.
.ScopeDescstring expression of the .Scope property. One of "PARAM", "LOCAL", "STATIC" or "MODULE".

An example:

With ErrEx.VariablesInspector
    While .IsEnd = False
        strDump = strDump & "(" & .ScopeDesc & ") " & .Name & " As " & .TypeDesc & " = " & .ValueDesc & vbCrLf 
    MsgBox strDump
End With

Similarly, this technique can be used on the ErrEx.CallStack.VariablesInspector instead whilst traversing through the call stack.

Example dumping the variables to a log table

Check out the Sample.mdb for an example of using the DumpAll method to conveniently dump all variables values as text.