To display the Immediate window at any time, you need to call the
FindWindow() API function to get the handle to the Immediate window,
and then call the ShowWindow() API function to make the actual window
visible. You can attach the Access Basic function that includes these
API functions to a command button or a RunCode action in the Autokeys
macro, or you can add it to your toolbar.
To display the Immediate window, follow these steps:
- Create a new module, or open an existing module.
- Add the following declarations to the Global section of the module.
NOTE: In the following sample code, an underscore (_) is used as a
line continuation character. Remove the underscore from the end of the
line when re-creating this code in Access Basic.
Option Explicit
Declare Function ShowWindow% Lib "user" (ByVal hWnd%, ByVal nCmd%)
Declare Function FindWindow% Lib "user" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any)
- Add the following ShowImmediateWindow() function:
Function ShowImmediateWindow ()
Dim IhWnd% ' Handle to the Immediate Window.
Dim ApiResults% ' Returns the previous state of IW.
Const MyNull = 0&
Const SW_SHOW = 5 ' Internal constant to show window.
Const ClassName = "OImmediate" ' Internal ClassName of IW.
IhWnd% = FindWindow(ClassName, MyNull)
If IhWnd% = 0 Then
MsgBox ("You need to open the IW once for this to work.")
End If
ApiResults% = ShowWindow(IhWnd%, SW_SHOW)
End Function
- Attach the code to a command button of a form.
- When you first start Microsoft Access, the Immediate window is not
displayed. When you open a module and display the Immediate window,
and then close it, Microsoft Access sets the window's Visible property
to False. Calling ShowWindow() resets the Visible property to True. If
you call this function without first displaying the Immediate window at
least once, you receive an error message because Microsoft Access has
not created the Immediate window yet and therefore cannot return a
window handle.
For information about displaying the Immediate window without
displaying the Module window, see the following article the Microsoft
Knowledge Base:
89594
(http://support.microsoft.com/kb/89594/EN-US/
)
ACC: How to Display Immediate Window Without Module
Window
With the information in the article referenced above, you can open
the Immediate window with macros once, and then use the code from this
article to display the Immediate window at any time.
- If you have registered the Immediate window with Windows by opening
it at least once, pressing the command button of the form while in
Datasheet view displays the Immediate window. Any Debug.Print
statements should then be visible.
NOTE: This is unsupported code, and there may be instances when this
example does not work.