UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> Immediate Window    
Immediate Window


What It Is

The Immediate Window is an invaluable VBA debugging tool that is part of the VBE (Visual Basic Editor). It allows you to evaluate expressions and run public procedures such as functions and subs without having to run your code in realtime (by opening the form that runs the code, for example). This is a very efficient means to quickly test code that you've written.


Where to Find It

To view the immediate window, press CTRL+G, or in the VBE Toolbar select View -> Immediate Window. By Default the window is shown docked along the bottom of the VBE.

How to Use It

There's two tasks for the Immediate Window. The first is to ask it a question, to which it will respond, and the second is to give it a command without requiring a response (such as running a subprocedure instead of a function).

To ask a question of the immediate window, place the cursor in the next free line and type a question mark followed by your expression or function name:


When you hit enter, the line will be evaluated and the return will be printed below it.


Note that the immediate window cannot work with multiple lines of code. For simplicity, it is recommended that you use only one line of code.

To run commands without requiring a return, simply omit the preceding question mark. This is useful for running subs or functions where you want to step through the code without requiring the return value:

DoCmd.OpenForm "MyForm"

Any built in or custom code can be run from the immediate window so long as it has public scope and doesn't need to be instantiated (ie - class modules -- see the Processing Multiple Statements section).


From within your code, you can make use of the Debug.Print statement to print information out to the immediate window. This is extremely helpful to determine the values of variables or function returns as you test your code without having to step through each line and hover over the variable with the mouse to see the value. For example:

Public Function TestMe()
 Debug.Print "Two plus Two is: " & 2+2
End Function

After your code runs, check the immediate window to see what values were printed.

Processing Multiple Statements

If you need to execute multiple statements or use object variables, you can separate statement with a colon. For example, to enumerate fields name in a table:

Set d = CurrentDb : For Each f In d.TableDefs("aTable").Fields : ?f.Name : Next


for i = 0 to 10: debug.Print i: next i

Note that Option Explicit does not apply to the Immediate Windows and you cannot execute all kind of statements; you cannot do a Dim statement, enable error handling and/or use some constructs such as Select Case in Immediate windows. The If/Then construct can be used only without an Else statement and must be in the single-line format:

a = 4: if a = 4 then debug.Print "a equals four"
a equals four
Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 21,670 times.  This page was last modified 01:00, 15 November 2012 by Jack Leach. Contributions by BananaRepublic  Disclaimers