The Code window is where you actually write
your code, including the subroutines, functions, and declarations. In
addition to the Code window, you'll use other components of the VBA
Editor to test and debug your code. The following sections look at each
of those components.
Like other things in database design, there is not
one definitive right answer for when to debug your VBA code; it's also
normal for developers to use different approaches depending on the
situation. You could debug as you write, testing every few lines. That
can be quite time-consuming, as you would have to run your code every
few lines (possibly with incomplete procedures) and make heavy use of
tools such as the Immediate window and Watch statements
discussed later in this section. The advantage of this approach is that
you always know the value of your variables, and the likelihood of
making or perpetuating a mistake is reduced.
Another approach is to write all the code for your
application and then debug it. Although this approach might seem
tempting because it doesn't require you to stop your coding to debug
your application, you can easily end up with numerous errors — some of
which could require you to make major changes to your code. Using that
technique can be like peeling an onion, particularly as the code becomes
more complex and interdependent, with one function calling another
function.
The best debugging approach falls somewhere between
those two options. Debugging as you go — testing and debugging your code
at the end of each procedure — enables you to be confident that each
procedure produces the appropriate and expected values. If you are
writing particularly long procedures, you may want to debug the
procedure more frequently. Otherwise, this approach should be sufficient
to ensure you're not in too deep with too many errors.
1. Immediate Window
The Immediate window in the Visual Basic Editor
enables you to enter commands and view the contents of variables while
your code is in break mode. Press Ctrl+G or select View => Immediate Window to open the window, as shown in Figure 2.
You can display the value of a variable in the Immediate window by either using the ? <Variable Name> or the Debug.Print <Variable Name>
in the window. Just type ? along with the variable name and press
Enter. VBA Editor displays the contents of the variable in the Immediate
window. For example, typing the following and pressing Enter displays
the value of intNumEmployee in the Immediate window:
? intNumEmployees
Seeing the current value of a variable can help you
troubleshoot code if you're encountering unexpected results. Simply set a
breakpoint in your code and test the value of a variable at any time.
This enables you to determine where in the code the value is being
incorrectly calculated. Using the question mark in the Immediate window
is shorthand for typing Debug.Print. So typing ? intNumEmployees or Debug.Print intNumEmployees produces the same results when you press Enter.
In addition to displaying the value of variables, you can also execute VBA commands in the Immediate window. Just eliminate the ? character and type the entire command, and then press Enter. Typing msgbox("Coffee or Tea?") and pressing Enter displays the message shown in Figure 3.
You can even perform calculations in the Immediate window such as:
? intTotalEmployees = intTempEmployees + intFullTimeEmployees.
2. The Debug.Print Statement
We previously mentioned using statements in the
Immediate window, they are also useful in other places. The following
module demonstrates how to use Debug.Print in your code. In looking at this example, you might imagine how Debug.Print can be helpful for testing and debugging.
Sub FunWithStringsAndNumbers()
Dim strBikes As String
Dim strCost As String
Dim strCustomerName As String
Dim intBikes As Integer
Dim curCost As Currency
strBikes = "19"
strCost = "74"
strCustomerName = "The Space Needle Seattle, WA"
intBikes = 19
curCost = 74
Debug.Print strBikes + strCost
Debug.Print intBikes + curCost
Debug.Print strCustomerName
End Sub
This code will show the following output in the Immediate window when you run it:
1974
93
The Space Needle Seattle, WA
Displaying the results of calculations or
values of variables in the Immediate Window is one of the ways you can
test your procedure. If the results aren't what you expect, you know
that you have a problem somewhere; you may need to determine if it is in
the initial data provided, the code, or the underlying logic.