Previous Page

Using the Immediate Pane

While you are creating and testing Visual Basic code, you may want to run a function or check the value of a control, field, or property. You can do this in the Immediate pane of the Debug window. The Immediate pane is a kind of scratch pad window in which expressions are evaluated immediately.

Although you can use the Immediate pane to run any Visual Basic command, you'll most commonly use it to:

 Test your Function and Sub procedures.

 Display the values of controls and properties.

 Evaluate expressions and display their results.

 Display debugging information from within your code.

Testing Procedures Using the Immediate Pane

The Immediate pane of the Debug window evaluates a Visual Basic expression or statement, such as a call to a Sub or Function procedure. You can evaluate an expression or a Function procedure by displaying the value it returns in the Immediate pane. You can test the possible effect of a Sub procedure with any given set of arguments by entering it as a statement in the Immediate pane, just as you would in the Module window.

You can display a value in the Immediate pane by using the Print method of the Debug object. The Debug object represents the Immediate pane in Visual Basic.

For example, suppose you have a function named DueDate, which takes an argument of type Date and returns the date of the first day of the month following the date that is passed to it:

Public Function DueDate(ByVal AnyDate As Date) As Variant
   ' This function calculates and returns the date of first
   ' day of month that follows the supplied date.
   DueDate = DateSerial(Year(AnyDate), Month(AnyDate) + 1, 1)
End Function

You can test the DueDate function by entering the following code in the Immediate pane:

Debug.Print DueDate(#4-12-96#)

Visual Basic runs the function and displays the result: 5/1/96.

Because the Debug object is the default object in the Immediate pane, you can use the Print method in the Immediate pane without referring to the Debug object. For example, you can test the DueDate function by entering the following statement in the Immediate pane:

Print DueDate(#4-12-96#)

You can use a question mark (?) as shorthand for the Print method. The question mark means precisely the same thing as Print and can be used in any context where Print is used. For example, you can determine the day of the week for the date returned by the DueDate function by entering the following code in the Immediate pane:

? Weekday(DueDate(#4-12-96#))

Visual Basic runs the function and displays the result: 4. This value indicates that May 1, 1996 is a Wednesday.

You can test a Sub procedure by entering it and its arguments in the Immediate pane. For example, you can test a procedure named SizeIt by entering the following code in the Immediate pane:

SizeIt 5000, 3000

You don't need to use the Print method because a Sub procedure doesn't return a value for the Immediate pane to display.

Note   In the Immediate pane, you can call procedures that are currently in scope without qualifying the procedure name. For example, you can call any procedure in the current module, and you can call any public procedure from any other module in the database. However, to run a private procedure in another module, you must qualify the name of the procedure with the name of the module that contains it. For example, to call a private procedure named AddValues in the Utilities module when the module is not open, you must type Utilities.AddValues in the Debug window. For information on the scope of procedures and variables, see Chapter 2, "Introducing Visual Basic" and Chapter 4, "Working with Variables, Data Types, and Constants."

You can run any built-in function or statement in the Immediate pane. However, a control structure is valid only if it can be completely expressed on one line. Because the For loop in the following example is on one line, it can be run in the Immediate pane.

For intI = 1 To 20 : Print 2 * intI : Next intI

See Also   For information on the Debug object or the Print method, search the Help index for "Debug object" or "Print method."

Displaying Values of Controls and Properties

You can evaluate any valid expression in the Immediate pane, including expressions involving the values and properties for Microsoft Access objects or Data Access Objects (DAO). For example:

? Forms!SalesReps.RecordSource
Employees

In the preceding example, the first line is the expression to be evaluated. The second line is what is displayed in the Immediate panethe value of the RecordSource property for the open SalesReps form.

Displaying Values in the Immediate Pane from Code

While you're testing your code, you may want to display the results of expressions in your code as it's running. You can use the Print method of the Debug object to display the results in the Immediate pane. The syntax for the Print method of the Debug object is:

Debug.Print [outputlist]

In this syntax, you can use the optional outputlist argument to specify an expression or list of expressions to print. For example, you can add the Print method of the Debug object to the DueDate function as follows:

Public Function DueDate(ByVal AnyDate As Date) As Variant
   ' This function calculates and returns the date of first
   ' day of month that follows the supplied date.
   Debug.Print "Year "; Year(AnyDate); "Month "; Month(AnyDate)
   DueDate = DateSerial(Year(AnyDate), Month(AnyDate) + 1, 1)
End Function

Now whenever this function is called, it displays the the current year and month in the Immediate pane, as well as the value returned by the DueDate function. For example, you can call this function from the Immediate pane and provide the date 4-12-96 for the AnyDate argument. If you're stepping through the code, as shown in the following illustration, the function prints the value for the year (1996) and the value for the month (4) to the Debug window once the line that contains the Debug.Print statement runs.

When you run the next line of code, the function evaluates the expression that determines the date of the first day of the following month. This is the value returned by the DueDate function. When the DueDate function has finished running, this value is also printed to the Debug window. In this case, the return value of the function is 5-1-96.

Displaying values in the Immediate pane from code has a couple of advantages. First, you don't have to suspend execution to get feedback on how your code is performing. You can view data or other messages as you run your code. Second, the feedback is displayed in a separate area (the Immediate pane), so it doesn't interfere with output you want users to see.

Once you're sure your code is working correctly, you can remove Debug.Print statements. Displaying values in the Immediate pane slows your code slightly, so you don't want to leave Debug.Print statements in your code when you don't need to use them. You can also use conditional compilation to specify when to include these statements and when to ignore them during compilation and at run time.

See Also   For more information on conditional compilation, see "Using Conditional Compilation" later in this chapter.

Note   The Debug window doesn't open automatically when Visual Basic encounters a Debug.Print statement. If you don't have the Debug window open, you won't see the values displayed by the Debug.Print statement. You can open the Debug window quickly by pressing CTRL+G.

Tips on Using the Immediate Pane

You can use the following shortcuts in the Immediate pane:

 Once you've run a statement in the Immediate pane, you can run it again by putting the insertion point anywhere in the statement and pressing ENTER.

 Before pressing ENTER, you can edit the statement in which the insertion point appears.

 You can use the mouse or the arrow keys to move the insertion point in the Immediate pane. Press ENTER only if the insertion point is on a statement you want to run.

 You can use the PAGE UP and PAGE DOWN keys within the Immediate pane to move through your code one page at a time. Pressing CTRL+ END moves the insertion point to the end of the Immediate pane.

 You can use the HOME key to move the insertion point to the beginning of the current line and the END key to move the insertion point to the end of the current line.

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender