Besides printing text messages, you can use the Print
to display the value of a variable. It is also possible to change the value of
a variable inside the Immediate window. The only catch is that the program must
be in Break mode to perform either of these actions.
If you think about it, one reason for this constraint is simple: A local variable
exists only in the context of the function in which it occurs. If the function
has already finished, the variable no longer exists. Forcing the program to enter
Break mode during the execution of a routine ensures that the variables in that
routine still exist for you to view or modify. Global variables and form-level
variables persist beyond particular functions, of course, but the rules apply
to all interactive uses of the Immediate window during program execution: Your
program must be in Break mode. Figure 18.18 provides an example.
FIGURE 18.18 Querying and changing a variable in the Immediate window.
In this case, the variable i is incremented in a
For loop. When the loop finishes, the Debug.Print statement
in the code displays the current value of the variable in the Immediate window,
and then the embedded Stop command throws the program into Break mode. At this
point, the programmer changes the value of i interactively in the Immediate window,
and then the value change is confirmed via the Debug.Print
method (which is invoked via the question mark). Sure enough, the value
has been changed from 101 to 10.
Besides modifying variables interactively, you can also change the values of
properties. If you want to hide the form whose code is currently running, for
example, you can type "me.Visible = False" in
the Immediate window. The form will vanish as soon as you press Enter. To restore
the form’s visibility, of course, you can type "me.Visible
= True". Again, your program must be in Break mode for this to work.
The capability to modify variables and properties in the Immediate window gives
you a chance to test a variety of conditions in your debugging sessions that your
program will face when it is released. Want to see what happens if a variable
value is (pick one) a specific value/very small/very large/empty/null? Modify
the variable and see what happens. Want to see what happens if you change a property
of a control or form? Go ahead. If you can do it in code, you can do it in the
NOTE - Ending Value of a Loop Counter Variable: In
the example in Figure 18.18, did you expect i to be
100 at the end of the loop? Remember that 100 is
the last legal value for the loop. When i equals
100, it is incremented one last time by the Next statement
in the final iteration. Thus when the loop exits, i equals