QuickStart: Variables Inspector (VBA)

 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.

What is the Variables Inspector?

The VariablesInspector object gives you the unique ability in your global error handler to inspect the values of all local variables in the procedure that caused the exception.

Not only that, but you can also traverse the stack and report the values of all local variables in all procedures that led up to the procedure of error being called!

We expose the VariablesInspector class object at two points - ErrEx.VariablesInspector and ErrEx.CallStack.VariablesInspector.

Requirements for this feature

Works with all editions of VBA6, even if running a compiled MDE or with the Access runtime.

Unfortunately this feature is not available for the SimplyVB6 edition of this product. VBA only.

Requires DISTRIBUTABLE EDITION to be purchased for reading or displaying the variable values.

The Variables Inspector from within the new Vista-style error dialog

To simply add a 'Show Variables' button to our Vista-style error dialog, just set the property ErrEx.DialogOptions.VariablesButtonEnabled to True and you will get a cool button added to your error dialog:

The rest of this page is for those that want to implement this functionality into their own custom error dialog forms or for logging purposes.

How to use the Variables Inspector (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 (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:

.Name string expression of the variable name
.TypeDesc string expression of the variable data type (e.g. 'DAO.Database', 'Long', 'String' etc)
.Value variant copy of the variable value (no matter what data type, all values are coerced into this Variant property)
.ValueDesc string 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.
.Scope enumeration to determine what type of variable this is - One of ParameterVariable, LocalVariable, StaticVariable or ModuleVariable
.ScopeDesc string 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 .DumpAll method and also for the advanced method of accessing the variables values (the Developer Access Form example in particular).