ACC2000: How to Retrieve the Microsoft Access Window Handle (210118)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210118
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to create a sample user-defined function to retrieve the handle to the Microsoft Access window. It also shows you how to create functions to determine and change the Microsoft Access window state (maximized, minimized, restored) as well as its size and position.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Every window in the Microsoft Windows environment has a unique number, or window handle, assigned to it. The window handle identifies the window and is a required argument for many Microsoft Windows application programming interface (API) functions.

How to Retrieve the Window Handle

To create the sample function GetAccesshWnd() that you can use to retrieve the Microsoft Access window handle, follow these steps:

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library, in which case 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. Start Microsoft Access, open any database, and then 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 or paste 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 (which has no parent window).
        While hWnd <> 0
            hWndAccess = hWnd
            hWnd = apiGetParent(hWnd)
        Wend
    
        GetAccesshWnd = hWndAccess
    
    End Function
    					
To test the GetAccesshWnd() function, follow these steps:
  1. Press CTRL+G to open the Immediate window.
  2. Type ? GetAccesshWnd() in the Immediate window, and then press ENTER. Note that the window handle for the Immediate window is returned.

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

  1. Add the following code 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 or paste 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 test the AccessMaximize functions, type AccessMaximize in the module's Immediate window, and then press ENTER.

    You can test the other functions similarly.

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

  1. Add the following code 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. Type or paste 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 test the IsAccessMaximized function, type ? IsAccessMaximized() in the module's Immediate window, and then press ENTER.

    This returns a True if the Microsoft Access window is maximized, or a False if it is not.

    You can test the other functions similarly.

How to Move and Size the Microsoft Access Window

  1. Add the following code 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. Type or paste 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 ? AccessMoveSize(0, 0, 640, 480) in the module's Immediate window, and then press ENTER.

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

REFERENCES

For more information about window handles, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type hwnd property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbProgramming KB210118