The Debug Assert Statement

You can just as easily type Debug.Assert in the Immediate window. 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 = 1000

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",

"Suits")

For i=1To intSuits

curTotalPrice = curTotalPrice + curSuitPrice

If curTotalPrice > curBudget Then

blnUnderBudget = False

Else

blnUnderBudget = True

End If

Debug.Assert blnUnderBudget

Next

End Sub

The code breaks every time you go over budget on your shopping trip. You can use this statement when testing for specific conditions within your code. Although Debug.Assert is a good debugging tool, you probably won't ever use it in live code because it's a rather abrupt way to stop an application. The user would get no warning and because the code stops, you do not get to provide him with a friendly message or explanation.

Was this article helpful?

0 0

Post a comment