The Debug Assert Statement

You might have noticed that when you were starting to type Debug.Print, you could have just as easily chosen to type Debug.Assert. This option conditionally suspends execution of code at the line where

Debug.Assert appears. For example, the following code uses the Debug.Assert statement to stop code execution when a specific condition is met:

Option Compare Database

Private blnUnderBudget As Boolean

Const curBudget = 10 0 0

Private Sub GoShopping()

Dim intSuits As Integer

Dim curSuitPrice As Currency

Dim curTotalPrice As Currency

Dim i as Integer

curSuitPrice = 100

intSuits = InputBox("Enter the desired number of suits",


For i = 1 To intSuits

curTotalPrice = curTotalPrice + curSuitPrice

If curTotalPrice > curBudget Then

blnUnderBudget = False


blnUnderBudget = True

End If

Debug.Assert blnUnderBudget


End Sub

This code will break every time you go over budget on your shopping trip. You can use this statement when testing for specific conditions within your code. While Debug.Assert is a good debugging tool, you probably won't ever use it in live code.

Was this article helpful?

0 0

Post a comment