Immediate Window

The Immediate window can be displayed in the Visual Basic Editor to allow you to enter commands and view the contents of variables while your code is in break mode. Press Ctrl+G or choose Immediate Window from the View menu to display the Immediate window in the Visual Basic Editor, as shown in Figure 5-5.

Figure 5-5

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 will display the contents of the variable in the Immediate window. For example, typing the following and pressing Enter will display the value of the intNumEmployees in the Immediate window.

? intNumEmployees

This can be helpful troubleshooting your 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 can allow 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 also type Debug.Print intNumEmployees and click 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, followed by pressing the Enter key. Typing msgbox("Replace with pithy message text.") and pressing Enter will display the message as shown in Figure 5-6.

You can even perform calculations in the Immediate window such as

intTotalEmployees = intTempEmployees + intFullTimeEmployees.

The Immediate window can do more than just display some simple calculations. It is 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's another method that is often used, especially by beginning developers. This other method involves placing message box code throughout your code to test the values of certain variables or calculations. There's nothing technically wrong with this method, but it can be messy and cumbersome. After all, when you're done debugging your code, you'll need to comment or remove all those message box calls. This can be a lot of unnecessary work when you could have just used some of the more widely accepted debugging methods.

0 0


  • Jimmy
    How to print Access 2003 values in immediate window?
    7 years ago

Post a comment