Assert method

Conditionally suspends execution when booleanexpression returns False at the line on which the method appears.

Syntax

object.Assert booleanexpression

The Assert method syntax has the following object qualifier and argument:

Part Description
object Required. Always the Debug object.
booleanexpression Required. An expression that evaluates to either True or False.

Remarks

Assert invocations work only within the development environment. When the module is compiled into an executable, the method calls on the Debug object are omitted.

All of booleanexpression is always evaluated. For example, even if the first part of an And expression evaluates False, the entire expression is evaluated.

Example

The following example shows how to use the Assert method. The example requires a form with two button controls on it. The default button names are Command1 and Command2.

When the example runs, clicking the Command1 button toggles the text on the button between 0 and 1. Clicking Command2 either does nothing or causes an assertion, depending on the value displayed on Command1. The assertion stops execution with the last statement executed, the Debug.Assert line, highlighted.

Option Explicit
Private blnAssert As Boolean
Private intNumber As Integer

Private Sub Command1_Click()
    blnAssert = Not blnAssert
    intNumber = IIf(intNumber <> 0, 0, 1)
    Command1.Caption = intNumber
End Sub

Private Sub Command2_Click()
    Debug.Assert blnAssert
End Sub

Private Sub Form_Load()
    Command1.Caption = intNumber
    Command2.Caption = "Assert Tester"
End Sub

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.