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 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:

Testing Procedures Using the Immediate Pane

You can use the Immediate pane of the Debug window to evaluate 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 procedure that uses different 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 date passed in the AnyDate argument.

	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(Date())

Visual Basic runs the function and displays the return value of the function as a date that is the first day of the month following the current date.

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(Date())

You can also use a question mark (?) as shorthand for the Print method in the Immediate pane. The question mark means 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(Date()))

Visual Basic runs the function and displays an integer that represents which day of the week the DueDate function returns.

You can test a Sub procedure by entering it and its arguments in the Immediate pane. For example, you could 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 this 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 that module is not the current module, 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, code entered in the Immediate pane is valid only if it can be completely expressed on one line. For example, because the For loop in the following example is entered 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, Data Access Objects (DAO), or any other object in a referenced library in the current database. 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 pane—the value of the RecordSource property for the open SalesReps form.

Displaying Values in the Immediate Pane from Code

When you’re dubugging your code, you may want to display the results of expressions in your code while it’s running. You can use the Print method of the Debug object to display data 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 runs, it displays the the current year and month in the Immediate pane. 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 while your code is running. 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’ve finished debugging your code and you are sure the code is working correctly, you should remove all 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 they are no longer needed. 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: