Harnessing the Power of the Immediate Window
The Immediate window serves several purposes. It provides you with a great way to test VBA and user-defined functions, it enables you to inquire about and change the value of variables while your code is running, and it enables you to view the results of Debug.Print statements. To open the Immediate window while in the Visual Basic Editor, do one of three things:
- Click the Immediate window tool on the Debug toolbar.
- Choose View, Immediate window.
- Press Ctrl+G.
NOTEAn advantage of pressing Ctrl+G is that it invokes the Immediate window without a Code window being active. You can click the Immediate window toolbar button or choose View, Immediate window only from within the VBE.Figure 15.1 shows the Immediate window.
Figure 15.1. The Immediate window enables you to test functions and to inquire about and change the value of variables.

NOTEThe Debug tools are available on a separate toolbar. To show the Debug toolbar, right-click any toolbar or menu bar and select Debug from the list of available toolbars.
Testing Values of Variables and Properties
The Immediate window enables you to test the values of variables and properties as your code executes. This can be quite enlightening as to what is actually happening within your code.To practice with the Immediate window, you do not even need to be executing code. To invoke the Immediate window while in a form, report, or module, press Ctrl+G. To see how this works, follow these steps:
Your screen should look like the one shown in Figure 15.2. You can continue to request the values of properties or variables within your VBA code.
Figure 15.2. Use the Immediate window to test the values of properties.

Setting Values of Variables and Properties
Not only can you display things in the Immediate window, you also can use the Immediate window to modify the values of variables and controls as your code executes. This feature becomes even more valuable when you realize that you can re-execute code within a procedure after changing the value of a variable. Here's how this process works:
Figure 15.3. Set the values of properties using the Immediate window.

Figure 15.4. The results of using the Immediate window to set the values of properties are shown here.

The Immediate window is an extremely valuable testing and debugging tool. The examples here barely begin to illustrate its power and flexibility.CAUTIONChanges you make to data while working in the Immediate window are permanent. On the other hand, Access does not save changes you make to the properties of controls or the values of variables with the form or report.Some people think that data changes made in the Immediate window are not permanent. In other words, if you modify the last name of a customer, they believe that the change will not be permanent (but, of course, it is). Other people think that if they change the BackColor property of a control, the change will persist in the design environment (but, of course, it won't).
Clearing the Immediate Window
The Immediate window displays the last 200 lines of output. As you add additional lines of code to the Immediate window, older lines disappear. When you exit completely from Access, it clears the Immediate window. If you want to clear the Immediate window at any other time, follow these three steps:
Practicing with the Built-In Functions
In addition to being able to test and set the values of properties and variables using the Immediate window, you can test any VBA function. To do so, type the function and its arguments in the Immediate window, preceded by a question mark. This code returns the month of the current date, for example:?datepart("m",date)
This tells you the date one month after today's date:?dateadd("m",1,date)
This tells you how many days exist between the current date and the end of the millennium:?datediff("d",date(),#12/31/2999#)
Executing Subroutines, Functions, and Methods
Notice the difference between how you call a function and how you call a subroutine. Because the function returns a value, you must call it using a question mark. On the other hand, when calling a subroutine, you use the Call keyword.NOTEYou also can call a subroutine from the Immediate window using this syntax:RoutineName Parameter1, Parameter2, ....
Notice that, when you omit the Call keyword, you do not need to enclose the parameters in parentheses.
Printing to the Immediate Window at Runtime
The capability to print to the Immediate window is useful because you can test what is happening as your code executes without having to suspend code execution. It also is valuable to be able to print something to a window when you are testing, without interfering with the user-interface aspect of your code. You can test a form without being interrupted and then go back and view the values of variables and so on. Here's how the process works:
Figure 15.5. Use Debug.Print statements to print values to the Immediate window.

NOTEAlthough it is good practice to remove Debug.Print statements when you have completed the debugging process, you can safely deploy your applications without removing them. Your users will never know that the statements are in your code unless they view the Immediate window. The Debug.Print statements result in only a minor degradation in performance.