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 O Immediate Window to open the window, as shown in Figure 4-4.

Immediate Window The Vba Edito
Figure 4-4

In the Immediate window, you can display the value of a variable by using the ? Debug.Print command. 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 will display the value of intNumEmployee in the Immediate window:

? intNumEmployees

Seeing the current value of a variable can be helpful when troubleshooting 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. The question mark is shorthand for typing Debug.Print. Instead of typing ? intNumEmployees, you can type Debug.Print intNumEmployees and press Enter. Both statements produce the same results.

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 msg-box("Tall or Grande?") and pressing Enter displays the message shown in Figure 4-5.

Microsoft Office Access * Tall or Grande?

Figure 4-5

You can even perform calculations in the Immediate window such as:

intTotalEmployees = intTempEmployees + intFullTimeEmployees.

The Immediate window is also a powerful debugging tool for your applications. For more information about the Immediate window, see Chapter 8.

Using the Immediate window along with other aspects of the VBA Editor detailed in this chapter, such as breakpoints and stepping through code, is the most generally accepted method of debugging your code. However, there are other options. One method that is often used by beginning developers is to place message box code throughout the code to test the values of selected variables or calculations. Although there is nothing technically wrong with this method, it can be messy and cumbersome. After all, when you're done debugging the code, you still need to comment out or remove all of the message box calls. That can be a lot of unnecessary work.

Was this article helpful?

0 0

Post a comment