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

ID: Q113881


The information in this article applies to:


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:

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.

  1. Create a new module.


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


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


  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 


  3. To run these functions, type ? <function name>() in the module's Immediate window.


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 IsIconic Lib "User" (ByVal hWnd As Integer) _
             As Integer
          Declare Function IsZoomed Lib "User" (ByVal hWnd As Integer) _
             As Integer 


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


  3. To run these functions, type ? <function name>() 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.


How to Move and Size the Microsoft Access Window:
  1. 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) 


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


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.


REFERENCES

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

Q147214 ACC95: How to Retrieve and Use the MS Access Window Handle

Additional query words: hWnd hWindow hWin


Keywords          : kbprg 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 2, 1999