ACC: How to Retrieve the MS Access Window Handle

ID: Q147214

The information in this article applies to:

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:

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.

1. Create a new module.

2. 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

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

1. Press CTRL+G to open the Debug window.

2. Type the following line in the Debug window, and then press ENTER:

      ? GetAccesshWnd()

   Note that the window handle for the Debug window is returned.

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

1. 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

2. 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

3. To run these functions, type the following in the module's Debug window,
   and then press ENTER

      ? <function name>()

       -or-

      <function name>

   where <function name> is the name of one of the functions in step 2.

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

1. 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

2. 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

3. To run these functions, type the following in the module's Debug window,
   and then press ENTER

      ? <function name>()


   where <function name> 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.

How to Move and Size the Microsoft Access Window:

1. 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

2. 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

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.

REFERENCES

For more information about Window handles in Microsoft Access version 2.0, please see the following article here in the Microsoft Knowledge Base.

    ARTICLE-ID: Q113881
    TITLE:      ACC: How to Retrieve the Microsoft Access Window
                Handle (1.x, 2.0)

Additional query words:
Keywords          : kbprg
Version           : 7.0, 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998