3. The Debug.Assert Statement
In addition to Debug.Print, you can also use Debug.Assert to pause the code execution when a Boolean value passed to the statement evaluated to False. For example, the following code stops the code execution when a blnUnderBudget is False:
Option Compare Database
Private blnUnderBudget As Boolean
Const curBudget = 1000
Sub GoShopping()
Dim intSuits As Integer
Dim curSuitPrice As Currency
Dim curTotalPrice As Currency
Dim curBudget As Currency
Dim i as Integer
curBudget = 1000
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 should not 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 exit gracefully by providing
him with a friendly message or explanation. However, the Debug.Assert
statement is essentially ignored in an MDE or ACCDE. That said, we also
caution you that you will need to use a conditional compilation to
essentially wrap the specified code in If, Then, and Else statements and
stipulate that they will be ignored. You can find additional guidance
about conditional compilations online by searching the Access
communities and help resources. It seems much easier to search and
comment out or remove the lines.
4. Breakpoints
Breakpoints are essentially places in your code where
you want to pause the execution of the code. For example, if you want
to check the value of the variable curTotalCost midway through the following procedure, you can use the Debug.Print statement as shown in the following code snippet or set a breakpoint, which we will demonstrate shortly.
Sub HowMuchCanWeSpend()
Dim curTotalPrice As Currency
Dim curUnitPrice As Currency
Dim intNumSocks As Integer
Dim i As Integer
curUnitPrice = 3.5
intNumSocks = InputBox( _
"Please enter the number of pairs of socks you want.", _
"Pairs of Socks")
For i=1 To intNumSocks
curTotalPrice = curTotalPrice + curUnitPrice
Next
Debug.Print curTotalPrice
End Sub
This code uses the Immediate window to display the amount you'll spend for the total sock purchase (using the line, Debug.Print curTotalPrice). That's great for a single transaction, but what if you want to see the total accumulating as you go? One option is to add a Debug.Print statement within the For...Next
loop. Alternatively, you can set a breakpoint anywhere in the loop.
When the breakpoint is reached, the code stops running and you can use
the Immediate window to obtain the value of any variable of interest (? <VariableName>). You can set a breakpoint on any line of code except for Dim statements and comments.
Setting a breakpoint is as simple as clicking in the
left margin of the Code window. A brick-colored dot appears in the
margin and the corresponding line of code is highlighted. To clear a
breakpoint, just click on the dot in the left margin. You can also set
and clear a breakpoint by placing the cursor in the desired line of code
and selecting Debug
Toggle Breakpoint or by pressing F9. When you run code that has
breakpoints, every time the breakpoint is reached, the code execution
stops and waits for you to decide (and tell it) what to do next. At that
point, you basically have four options:
Check the value of variables in the Immediate
window. When your code reaches a breakpoint, the value of all variables
is retained. You can check the value of any variable by using the Debug.Print statement or the ? character within the Immediate window ..
Use
your mouse to hover over any variable in the current procedure. The
value of the variable appears close to the mouse cursor. Keep in mind
that variables will show no value when you hover over them if a value
was not assigned to them before you reached the break point,
Press F5 or select Run Continue to continue code execution. The code will run until it reaches the next breakpoint or the end of the procedure.
Press
F8 to step through the code (explained in the following section). When
you press F8, the code execution stops at the next line of code — even
if it does not have a breakpoint. This enables you to step through the
code and identify missed steps or other errors. As you step through the
code, you can continue to check the value of the variables or press F5
to resume code execution.
When VBA encounters a breakpoint, it pauses
execution immediately before the line of code executes. The line of code
that contains the breakpoint is not executed unless or until you choose
to step through the code using the F8 key (or continue running the
code).
5. Stepping through Code
In most cases, you design code to run with little or
no user intervention. However, when testing the code, it is often
helpful or even necessary to do more than insert a couple of breakpoints
or include some Debug.Print statements. If your code has
several variable changes and/or some intricate looping, it can help
clarify the process to step through the code line by line. This allows
you to validate the value of variables after each line of code has
executed, and it can help you pinpoint any errors or mistakes in the
underlying logic.
To start stepping through your code, place the cursor
at the point that you want to initiate the process and press F8 (you
can also press F8 after the code has entered break mode to step through
the remaining code). When you press F8 to begin code execution, the name
of the sub or function will be highlighted in yellow. Pressing the F8
key moves the code execution from line to line, sequentially
highlighting the next executable line. Much like with breakpoints,
comment lines and Dim statements are skipped when stepping through code. As you press F8, the highlighted line executes.
Stepping through code is an important tool so it is
worth reiterating how the process works. The first pressing of F8
highlights the next executable code; the subsequent pressing of F8
executes the highlighted code. If nothing is highlighted, pressing F8
highlights code; if something is highlighted, pressing F8 runs it.
|
If the current procedure calls another sub or
function, F8 also executes the called procedure line-by-line. If you
already tested the called procedure and are confident it doesn't contain
any errors, you can execute the entire called procedure and then return
to line-by-line execution of the calling procedure by pressing
Shift+F8. This is called stepping over the procedure.
Stepping over the called procedure executes the
entire procedure and then returns to the calling procedure to continue
with code execution one step at a time. If you're within a called
procedure, you can press Ctrl+Shift+F8 to step out
of the current procedure. This may sound like nothing more than
semantics. And indeed, if you're already in the called procedure,
stepping out has the same effect as stepping over. But, the two actions
will not always provide the same results. We'll use the following
example to illustrate the difference and to give you some practice with
code. Assume you're stepping through the following code:
Option Compare Database
Private blnUnderBudget As Boolean
Const curBudget As Currency = 1000
Private Sub GoShopping()
Dim intSuits As Integer
Dim curSuitPrice As Currency
Dim curTotalPrice As Currency
Dim curBudget As Currency
Dim i As Integer
curBudget = 1000
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
Next
If blnUnderBudget = False Then
OverBudget
End If
End Sub
Private Sub OverBudget()
Debug.Print "You've gone over budget."
Debug.Print "You need to work some overtime."
Debug.Print "Remember to pay your taxes."
End Sub
Use the F8 key to step through the code until you reach the last If...Then loop (If blnUnderBudget = False Then). When the OverBudget line is highlighted in yellow (meaning it hasn't yet been executed), stepping over the OverBudget procedure returns execution to the line after the OverBudget call (in this case the End If line). If you step out of the procedure, the OverBudget procedure runs, and your code returns to the GoShopping
procedure and completes the procedure. If, however, you use the F8 key
to step through the code until you reach the first line of the OverBudget procedure, stepping out of the procedure returns you to the line after the OverBudget called (the End If line).
DEBUGGING TECHNIQUE | DESCRIPTION | KEYBOARD SHORTCUT |
---|
Step Into | Executes the next line of code in your procedure (highlights the line in yellow). | F8 |
Step Over | Executes
code one line at a time within the current procedure. If a second
procedure is called from within the first, the entire second procedure
is executed at once. | Shift+F8 |
Step Out | VBA
executes the remainder of the current procedure. If you are within the
second procedure and then Step Out, the entire second procedure is
executed and code execution continues in the first procedure — on the
line following the line that called the second procedure. | Ctrl+Shift+F8 |