You are currently offline, waiting for your internet to reconnect

ACC2000: How to Retrieve the Microsoft Access Window Handle

This article was previously published under Q210118
This article has been archived. It is offered "as is" and will no longer be updated.
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 toretrieve 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, orwindow 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 ExplicitDeclare Function apiGetActiveWindow Lib "user32" Alias "GetActiveWindow" () As LongDeclare 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 = hWndAccessEnd 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 LongGlobal Const SW_MAXIMIZE = 3Global Const SW_SHOWNORMAL = 1Global Const SW_SHOWMINIMIZED = 2					
  2. Type or paste the following functions in the module:
    Function AccessMinimize()    AccessMinimize = apiShowWindow(GetAccesshWnd(), SW_SHOWMINIMIZED)End FunctionFunction AccessMaximize()    AccessMaximize = apiShowWindow(GetAccesshWnd(), SW_MAXIMIZE)End FunctionFunction 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 LongDeclare 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 IfEnd FunctionFunction IsAccessMinimized ()    If apiIsIconic(GetAccesshWnd()) = 0 Then        IsAccessMinimized = False    Else        IsAccessMinimized = True    End IfEnd FunctionFunction IsAccessRestored ()    If IsAccessMaximized() = False And _         IsAccessMinimized() = False Then        IsAccessRestored = True    Else        IsAccessRestored = False    End IfEnd 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, TrueEnd Function					
To move the Microsoft Access window to the upper-left corner of the screenand 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 givesthe Microsoft Access window the appearance of being maximized, although itis really restored and sized to fill the screen. Note that the dimensionsthat 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.
Properties

Article ID: 210118 - Last Review: 12/05/2015 11:24:32 - Revision: 2.0

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbprogramming KB210118
Feedback