Microsoft KB Archive/147214

= ACC: How to Retrieve the MS Access Window Handle =

Article ID: 147214

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q147214





SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to create a sample user-defined function to retrieve the handle to the Microsoft Access window. This article also shows you to use the sample function to do the following:


 * Minimize, maximize, and restore the Microsoft Access window.
 * Determine if the Microsoft Access window is minimized, maximized, or restored.
 * Move and size the Microsoft Access window.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access for Windows 95" manual.



MORE INFORMATION
Every window in the Microsoft Windows environment has a unique number, or window handle, assigned to it that is used to identify the window. The window handle is a required argument for many Microsoft Windows application programming interface (API) functions.

The following steps show you how to create the sample function, GetAccesshWnd, that you can use to retrieve the Microsoft Access window handle.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

 Create a new module.  Add the following lines to the module's Declarations section: Option Explicit

Declare Function apiGetActiveWindow Lib "user32" Alias _ "GetActiveWindow" As Long Declare Function apiGetParent Lib "user32" Alias "GetParent" _ (ByVal hwnd As Long) As Long   Type the following function in the module: Function GetAccesshWnd Dim hWnd As Long Dim hWndAccess As Long

' Get the handle to the currently active window. hWnd = apiGetActiveWindow hWndAccess = hWnd

' Find the top window without a parent window. While hWnd <> 0 hWndAccess = hWnd hWnd = apiGetParent(hWnd) Wend

GetAccesshWnd = hWndAccess

End Function 

How to Use the GetAccesshWnd Function
General Use:

 Press CTRL+G to open the Debug window. Type the following line in the Debug window, and then press ENTER:

? GetAccesshWnd

Note that the window handle for the Debug window is returned.</li></ol>

How to Minimize, Maximize, or Restore the Microsoft Access Window:

  Add the following to the module's Declarations section: Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _ (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Global Const SW_MAXIMIZE = 3 Global Const SW_SHOWNORMAL = 1 Global Const SW_SHOWMINIMIZED = 2 </li>  Type the following functions in the module: Function AccessMinimize AccessMinimize = apiShowWindow(GetAccesshWnd, SW_SHOWMINIMIZED) End Function

Function AccessMaximize AccessMaximize = apiShowWindow(GetAccesshWnd, SW_MAXIMIZE) End Function

Function AccessRestore AccessRestore = apiShowWindow(GetAccesshWnd, SW_SHOWNORMAL) End Function </li> To run these functions, type the following in the module's Debug window, and then press ENTER

?

-or-

where is the name of one of the functions in step 2.</li></ol>

How to Determine If the Microsoft Access Window Is Minimized, Maximized, or Restored:

  Add the following to the module's Declarations section: Declare Function apiIsIconic Lib "user32" Alias "IsIconic" (ByVal _        hwnd As Long) As Long Declare Function apiIsZoomed Lib "user32" Alias "IsZoomed" (ByVal _        hwnd As Long) As Long </li>  Enter the following functions in the module: Function IsAccessMaximized If apiIsZoomed(GetAccesshWnd) = 0 Then IsAccessMaximized = False Else IsAccessMaximized = True End If     End Function

Function IsAccessMinimized If apiIsIconic(GetAccesshWnd) = 0 Then IsAccessMinimized = False Else IsAccessMinimized = True End If     End Function

Function IsAccessRestored If IsAccessMaximized = False And _ IsAccessMinimized = False Then IsAccessRestored = True Else IsAccessRestored = False End If     End Function </li> To run these functions, type the following in the module's Debug window, and then press ENTER

?

where is the name of one of the functions in step 2. This will return a True if the Microsoft Access window is maximized, or a False if it is not.</li></ol>

How to Move and Size the Microsoft Access Window:

  Add the following to the module's Declarations section: Declare Function apiMoveWindow Lib "user32" Alias "MoveWindow" _ (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal _        nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) _ As Long </li>  Enter the following function in the module: Function AccessMoveSize (iX As Integer, iY As Integer, iWidth As _        Integer, iHeight As Integer) apiMoveWindow GetAccesshWnd, iX, iY, iWidth, iHeight, True End Function </li></ol>

To move the Microsoft Access window to the upper-left corner of the screen and size it to the standard VGA display size of 640 x 480 pixels, type the following in the module's Debug window:

? AccessMoveSize(0, 0, 640, 480)

On a computer configured with the standard VGA video driver, this will give the Microsoft Access window the appearance of being maximized, although it is really restored and sized to fill the screen. Note that the dimensions you supply to this function are in pixels.

<div class="references_section">