TAKING ADVANTAGE OF THE IMMEDIATE WINDOW

The VBE Immediate window has a good number of features that allow you to follow code and to examine expressions in a number of different ways. To bring the Immediate window up while in the VBE, open the View menu and choose Immediate Window.

Tip

You can press Ctrl+G anywhere in your application to bring up the Immediate window with the VBE.


By using the Immediate window, you can print data and reassign values to variables and Access objects. This comes in handy as you are debugging your application.

Printing Data to the Immediate Window From Your Application

You can print information to the Immediate window by using the Print method available on the Debug. When a line of code uses the Debug.Print method, it prints the data to the Immediate window, even if the window isn't open. The next time you open the window, the text will be there.

Note

Although you can't see the code lines that contain the Debug.Print statement if the Immediate window isn't open, Access still has to take the time to parse the command at runtime and print to the object. If you don't want to include these code lines, use the conditional compilation directives mentioned later in the section “Controlling Code with Conditional Compilation Commands.”


Figure A.4 shows the syntax for including the Debug.Print method in your code, as well as the output in the Immediate window.

Figure A.4. The Debug.Print method is useful for keeping track of expressions in your application.


Displaying Data While in the Immediate Window

By using the ? (Print) statement, you can display all types of expressions while in an application. Simply place a Stop statement or breakpoint in your application, and then open the Immediate window and print the information you're interested in by using the ? statement. (Stop statements and breakpoints are discussed to a greater detail later in the section “Setting Breakpoints and Using the Stop Statement.”)

Suppose that you're running a routine that runs through a recordset and checks a value for one of the fields. By displaying the value of that field to the Immediate window, you could see what the value is during execution for each record.

The following are some examples of different types of data that you can display while in the Immediate window.

Object Type Syntax
Control on a form ? Forms!FormName!ControlName
Property of a control ? Forms!FormName!ControlName.PropertyName
Variable ? VariableName
Variable in an expression ? VariableName * 20

Assigning Values to Variables and Objects in the Immediate Window

The Immediate window also lets you assign values and perform commands right from the window.

Note

Through the Immediate window, you can assign a new value to a variable, and then use the Run menu's Set Next Statement command to place the next line of execution just before that value is used in the program. You can then use the Step commands to walk through the code and view the results. The Step commands are discussed in greater detail later in the section “Debugging One Step at a Time.”


By using the following table, you can turn around and use similar syntax for assigning values:

Object Type Syntax
Control on a form Forms!FormName!ControlName = 1
Property of a control
Forms!FormName!ControlName.
											PropertyName = "New Name"

Variable VariableName = 3
Variable in an expression VariableName = VariableName * 20

Actions such as closing recordsets manually can also be performed.

Running Code from the Immediate Window

One nice thing about the Immediate window is that it gives you the capability to test routines without having to have a complete framework around the routine. You can call routines straight from the Immediate window by simply typing the necessary syntax in the window. The syntax for calling the two types of procedures is as follows:

  • For a subroutine, the syntax is

    									SubName
    									arg1,arg2...
    								

    or

    Call SubName (arg1,arg2...)
    

  • For a function, the syntax is

    ? FunctionName(arg1,arg2...)
    

    if you want to print the return value of the function. Or

    									FunctionName arg1,arg2...
    								

    if you're just running the function and don't care about the results.

Note

The functionality of running functions on a line by themselves was new as of Access 95. Before that, functions couldn't be called on a line by themselves. This also is true when calling functions at runtime.


Figure A.5 shows an example of calling a subroutine and a function.

Figure A.5. Use the Immediate window to test subroutines and functions on the fly.


For more information about functions and subroutines, refer to Chapter 2. This appendix continues to revisit the Immediate window by expanding coverage of its capabilities with other debugging tools found in Access 2000.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.35.77