MS-Access / Getting Started

The Debug Object

The Debug object is common with VBA programmers for troubleshooting problems by sending output to the Immediate window. The Debug object has two methods: Print and Assert. The Print method prints the value of its parameter and sends it to the Immediate window.

The Assert method conditionally breaks program execution when the method is reached. More specifically, the Assert method takes an expression as a parameter, which evaluates to a Boolean value. If the expression evaluates to False, the program's execution is paused. Otherwise, program execution continues. The next procedure demonstrates the use of the Assert method.

Private Sub cmdDivide_Click()

    Dim passedTest As Boolean

    On Error GoTo ErrorBin

    If Val(txtOperand2.Value) = 0 Then

	passedTest = False

Else

    passedTest = True
End If

' Conditionally pause program execution.
Debug.Assert passedTest

MsgBox "Result is " & Val(txtOperand1.Value) _
 / Val(txtOperand2.Value)

    Exit Sub

ErrorBin:

    MsgBox "Error Number " & Err.Number & ", " & Err.Description

    Resume Next

End Sub
[Previous] [Contents] [Next]