Learn Access 2003 VBA with The Smart Method
80
www.LearnAccessVBA.com
Lesson 4-5: Use the immediate
window to view and change
variable contents
We’ve already seen how easy it is to set breakpoints and to step through
code using the debug tools.
Programmers often want to examine the contents of different variables at
particular points in code execution. The Immediate window lets you do
just that. It’s hard to exaggerate how useful the Immediate window is
when de-bugging code.
1
Set a break point in your code on the Beep command in the
ThankYouMessage sub routine.
2
Click the command button on your frmTest form to begin
code execution.
The code editor window opens with code execution halted at the
Beep command.
3
Enable the Immediate Window if it is not already visible.
The immediate window will be at the bottom of the code window
if it is enabled. If not select View Immediate Window from the
main menu to make it appear.
4
Display the contents of the strMessage variable in the
immediate window.
Session4b
pg_0002
Session Four: An Introduction To VBA
© 2007 The Smart Method Ltd
81
Type the text . strMessage in the immediate window and then
press the <Enter> key. The question mark is a throw-back to the
very first version of the Basic language and means “Print". You
will use the question mark a lot in the immediate window.
The contents of the variable strMessage is displayed.
5
Display the literal result of a calculation in the Immediate
window.
You can type any arithmetic expression into the immediate
window. For example type . 10/3 and then press the <Enter> key.
The answer is displayed.
6
Change the value of the strMessage variable using the
immediate window.
Sometimes it is useful to change the values of a variable at a break
point in your code.
Type strMessage = “Quite amazing" in the immediate window
followed by the <Enter> key.
7
View the new value of strMessage using an Auto Data Tip.
If you hover the mouse cursor over either instance of strMessage
within your code an Auto Data Tip will appear showing the
current contents of the variable.
8
Click the Continue button
on the Debug toolbar to
execute the remainder of the code.
The message box is displayed with the new message.