ACC: How to Get Form's Right and Down Measurements (95/97) (143162)



The information in this article applies to:

  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97

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

SUMMARY

This article shows you how to create a Visual Basic for Applications Sub procedure called GetFormDimensions to get a form window's right and down measurements so that you can use them in the MoveSize macro action. This article also demonstrates how to position a form immediately below or to the right of a second form.

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 your version of the "Building Applications with Microsoft Access" manual.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

150895 ACC95: Microsoft Access Sample Forms Available in Download Center

175066 ACC97: Microsoft Access 97 Sample Forms Available in Download Center

MORE INFORMATION

In Microsoft Access, you can use the WindowWidth and WindowHeight form properties to determine the current width and height of the window that a form is contained in. However, there are no properties to determine the window's right and down measurements (these measurements are commonly used in the MoveSize macro action to position a form window).

The right and down measurements, along with the width and height, are useful if you want to position a form side-by-side with another form. The following examples show you how to create and use the GetFormDimensions Sub procedure to get these measurements and to use them to position a form to the right of, and then below, another form by using the MoveSize macro action.

How to Create the Sub Procedure

  1. Open the sample database Northwind.mdb.
  2. Create a new module and enter the following lines in the module's Declarations section:
          Option Explicit
    
          Type RECT_Type
              left As Long
              top As Long
              right As Long
              bottom As Long
          End Type
    
          Declare Function apiGetWindowRect Lib "user32" Alias _
          "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT_Type) As Long
          Declare Function apiGetDC Lib "user32" Alias "GetDC" _
          (ByVal hWnd As Long) As Long
          Declare Function apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal _
          hWnd As Long, ByVal hDC As Long) As Long
          Declare Function apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" _
          (ByVal hDC As Long, ByVal nIndex As Long) As Long
          Declare Function apiGetActiveWindow Lib "user32" Alias _
          "GetActiveWindow" () As Long
          Declare Function apiGetParent Lib "user32" Alias "GetParent" (ByVal _
          hWnd As Long) As Long
          Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" _
          (ByVal hWnd As Long, ByVal lpClassName As String, ByVal _
          nMaxCount As Long) As Long
    
          Global Const TWIPSPERINCH = 1440
    					
  3. Create the following procedures in the module:
          Sub GetFormDimensions(F As Form, FLeft As Long, FTop As Long, _
             FWidth As Long, FHeight As Long)
          '*************************************************************
          ' PURPOSE: Returns the left, top, width, and height
          '          measurements of a form window in tdwips.
          ' ARGUMENTS:
          '    F: The form object whose measurements are to be determined.
          '    FLeft, FTop, FWidth, FHeight: Measurement variables
          '    that will return the dimensions of form F "by reference."
          ' NOTE: The FWidth and FHeight values will be equivalent to
          '    those provided by the form WindowWidth and WindowHeight
          '    properties.
          '*************************************************************
             Dim FormRect As RECT_Type
             Dim MDIClient As RECT_Type
             Dim MDIClientLeft As Long
             Dim MDIClientTop  As Long
    
             ' Get the screen coordinates and window size of the form.
             ' The screen coordinates are returned in pixels measured
             ' from the upper-left corner of the screen.
             apiGetWindowRect F.hWnd, FormRect
             FLeft = FormRect.left
             FTop = FormRect.top
             FWidth = FormRect.right - FormRect.left
             FHeight = FormRect.bottom - FormRect.top
    
             ' Convert the measurements from pixels to twips.
             ConvertPIXELSToTWIPS FLeft, FTop
             ConvertPIXELSToTWIPS FWidth, FHeight
    
             ' If the form is not a pop-up form, adjust the screen
             ' coordinates to measure from the top of the Microsoft
             ' Access MDIClient window. Position 0,0 for a pop-up form
             ' is the upper-left corner of the screen, whereas position
             ' 0,0 for a normal window is the upper-left corner of the
             ' Microsoft Access client window below the menu bar.
             If GetWindowClass(F.hWnd) <> "OFormPopup" Then
                ' Get the screen coordinates and window size of the
                ' MDIClient window.
                apiGetWindowRect apiGetParent(F.hWnd), MDIClient
                MDIClientLeft = MDIClient.left
                MDIClientTop = MDIClient.top
                ConvertPIXELSToTWIPS MDIClientLeft, MDIClientTop
    
                ' Adjust the form dimensions from the MDIClient
                ' measurements.
                FLeft = FLeft - MDIClientLeft
                FTop = FTop - MDIClientTop
             End If
          End Sub
    
          Sub ConvertPIXELSToTWIPS(X As Long, Y As Long)
          '*************************************************************
          ' PURPOSE: Converts the two pixel measurements passed as
          '          arguments to twips.
          ' ARGUMENTS:
          '    X, Y: Measurement variables in pixels. These will be
          '          converted to twips and returned through the same
          '          variables "by reference."
          '*************************************************************
             Dim hDC As Long, hWnd As Long, RetVal As Long
             Dim XPIXELSPERINCH, YPIXELSPERINCH
             Const LOGPIXELSX = 88
             Const LOGPIXELSY = 90
    
             ' Retrieve the current number of pixels per inch, which is
             ' resolution-dependent.
             hDC = apiGetDC(0)
             XPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSX)
             YPIXELSPERINCH = apiGetDeviceCaps(hDC, LOGPIXELSY)
             RetVal = apiReleaseDC(0, hDC)
    
             ' Compute and return the measurements in twips.
             X = (X / XPIXELSPERINCH) * TWIPSPERINCH
             Y = (Y / YPIXELSPERINCH) * TWIPSPERINCH
          End Sub
    
          Function GetWindowClass(hWnd As Long) As String
          '*************************************************************
          ' PURPOSE: Retrieve the class of the passed window handle.
          ' ARGUMENTS:
          '    hWnd: The window handle whose class is to be retrieved.
          ' RETURN:
          '    The window class name.
          '*************************************************************
             Dim Buff As String
             Dim BuffSize As Integer
             Buff = String$(255, " ")
             BuffSize = apiGetClassName(hWnd, Buff, 255)
             GetWindowClass = left$(Buff, BuffSize)
          End Function
    					

How to Use the GetFormDimensions Sub Procedure

  1. Open the sample database Northwind.mdb.
  2. Add the following Sub procedure to the module that you created in step 2 of the previous section:
          Sub MoveEmployeesAround ()
             Dim frmCust As Form
             Dim frmEmp As Form
             Dim frmCustLeft As Long
             Dim frmCustTop As Long
             Dim frmCustWidth As Long
             Dim frmCustHeight  As Long
             Dim frmEmpLeft As Long
             Dim frmEmpTop As Long
             Dim frmEmpWidth As Long
             Dim frmEmpHeight  As Long
    
             Set frmCust = Forms!Customers
             Set frmEmp = Forms!Employees
    
             GetFormDimensions frmCust, frmCustLeft, frmCustTop, _
                frmCustWidth, frmCustHeight
             GetFormDimensions frmEmp, frmEmpLeft, frmEmpTop, _
                frmEmpWidth, frmEmpHeight
    
             frmEmp.SetFocus
    
             MsgBox "Move Employees to the right of Customers!"
             DoCmd.MoveSize frmCustLeft + frmCustWidth, frmCustTop
    
             MsgBox "Move Employees below Customers!"
             DoCmd.MoveSize frmCustLeft, frmCustTop + frmCustHeight
          End Sub
    					
  3. Open the Customers and Employees forms in Form view.
  4. Open the Debug window by pressing CTRL+G. Type the following line, and then press ENTER:
          MoveEmployeesAround
    					
  5. In the message box that appears, click OK. The Employees form will be positioned immediately to the right of the Customers form, and then another message box will appear.
  6. In the second message box, click OK. The Employees form will be positioned immediately below the Customers form.
NOTE: Depending on the position and size of the Customers form, the Employees form may be positioned so that you cannot see it.

REFERENCES

For more information about how to get form window's right and down measurements in Microsoft Access 2.0, please see the following article in the Microsoft Knowledge Base:

121100 ACC2: How to Get Form's Right and Down Measurements

For more information about the MoveSize macro action, search the Help Index for MoveSize or ask the Microsoft Access 97 Office Assistant.

For more information about the WindowHeight and WindowWidth properties, search the Help Index for WindowHeight or WindowWidth or ask the Microsoft Access 97 Office Assistant.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbProgramming KB143162