Microsoft KB Archive/113881

= ACC: How to Retrieve the MS Access Window Handle (1.x/2.0) =

Article ID: 113881

Article Last Modified on 5/6/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q113881



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

This article describes a sample user-defined Access Basic function that you can use to get the handle to the Microsoft Access window. This article also describes how to use the sample function to:


 * 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 Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.



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 describe how to create the sample function GetAccesshWnd that you can use to get 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.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.

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

Option Explicit

Declare Function GetActiveWindow Lib "User" As Integer Declare Function GetParent Lib "User" (ByVal hWnd As Integer) _ As Integer   Enter the following function in the module:

Function GetAccesshWnd Dim hWnd As Integer Dim hWndAccess As Integer

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

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

GetAccesshWnd = hWndAccess

End Function 

How to Use the GetAccesshWnd Function
General Use:


 * 1) Choose Immediate Window from the View menu in the module's Design view.
 * 2) Type the following and then press ENTER:

? GetAccesshWnd

The window handle for the Immediate window will be returned.

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

  Add the following to the module's Declarations section:

Declare Function ShowWindow% Lib "User" (ByVal hWnd%, _        ByVal nCmdShow%)

Global Const SW_MAXIMIZE = 3 Global Const SW_SHOWNORMAL = 1 Global Const SW_SHOWMINIMIZED = 2   Enter the following functions in the module:

Function AccessMinimize AccessMinimize = ShowWindow(GetAccesshWnd, SW_SHOWMINIMIZED) End Function

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

Function AccessRestore AccessRestore = ShowWindow(GetAccesshWnd, SW_SHOWNORMAL) End Function  To run these functions, type ? in the module's Immediate window.</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 IsIconic Lib "User" (ByVal hWnd As Integer) _ As Integer Declare Function IsZoomed Lib "User" (ByVal hWnd As Integer) _ As Integer </li>  Enter the following functions in the module:

Function IsAccessMaximized If IsZoomed(GetAccesshWnd) = 0 Then IsAccessMaximized = False Else IsAccessMaximized = True End If     End Function

Function IsAccessMinimized If IsIconic(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 ? in the module's Immediate window. For example, type

? IsAccessMaximized

to return a -1 if the Microsoft Access window is maximized, or a 0 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 Sub MoveWindow Lib "User" (ByVal hWnd As Integer, _        ByVal X As Integer, ByVal Y As Integer, _         ByVal nWidth As Integer, ByVal nHeight As Integer, _         ByVal bRepaint As Integer) </li>  Enter the following function in the module:

Function AccessMoveSize (iX As Integer, iY As Integer, _        iWidth As Integer, iHeight As Integer) MoveWindow 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 Immediate 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">