Position Form RRS feed

  • Question

  • Thanks for taking the time to read my question

    I have a main form with a button. the button opens another form. I want to position the second form so that it opens vertically at the same position as the first form.

    The button sends OpenArgs to the second form which include Me.WindowTop of the first form.

    The 'odd' part is that Me.WindowTop of the first form is 500 and when I use this number in

    Docmd.MoveSize ,500

    the second form is way to high. I manually edited the number and 4000 seems to work better.

    I would leave the static value in there however users will have different screens and resolutions so I'm not sure that 4000 will work for everyone.

    How do I make the second form open at the same vertical position as the first form?


    Wednesday, February 27, 2019 2:41 PM

All replies

  • This is from an old Microsoft sample database.

    In a standard module:

    Type Rect_Type
      Left As Long
      Top As Long
      Right As Long
      Bottom As Long
    End Type
    Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As Rect_Type) As Long
    Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
    Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
    Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
    Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
      (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Global Const TwipsPerInch = 1440
    Global Const LogPixelsX = 88
    Global Const LogPixelsY = 90
    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 twips.
    '  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.
      GetWindowRect 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.
        GetWindowRect GetParent(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.
    '  X, Y: Measurement variables in pixels. These will be converted to twips and returned through the same variables "by reference."
      Dim hDC As Long
      Dim hWnd As Long
      Dim XPixelsPerInch As Long
      Dim YPixelsPerInch As Long
      ' Retrieve the current number of pixels per inch, which is resolution-dependent.
      hDC = GetDC(0)
      XPixelsPerInch = GetDeviceCaps(hDC, LogPixelsX)
      YPixelsPerInch = GetDeviceCaps(hDC, LogPixelsY)
      ReleaseDC 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.
    '  hWnd: The window handle whose class is to be retrieved.
    ' RETURN:
    '  The window class name.
      Dim Buff As String
      Dim BuffSize As Integer
      Buff = Space$(255)
      BuffSize = GetClassName(hWnd, Buff, 255)
      GetWindowClass = Left$(Buff, BuffSize)
    End Function

    In the form module of the first form:

    Private Const conFudge = 30
    Dim Form1Left As Long
    Dim Form1Top As Long
    Dim Form1Width As Long
    Dim Form1Height As Long
    Private Sub cmdOpenForm_Click()
      GetFormDimensions Me, Form1Left, Form1Top, Form1Width, Form1Height
      DoCmd.OpenForm "Form2"
      DoCmd.MoveSize , Form1Top - conFudge
    End Sub

    Here, cmdOpenForm is the command button that opens the second form, named Form2 in this example.

    Regards, Hans Vogelaar (

    Wednesday, February 27, 2019 4:11 PM
  • Hi Hans,

    Thank you so much for this code!

    I've implemented and I get the following result

    debug.Print Form1Top
    debug.Print me.WindowTop

    I'm still getting a value in the 500 range... Is there something "funny" with my first form that the top of it is not registering properly?

    Thanks again.

    Wednesday, February 27, 2019 4:57 PM
  • Hi,

    Further testing has revealed that Forms!Form2.SetFocus does not change the focus and when the docmd.MoveSize is run, it's being applied to form1.

    I have also changed the code to Forms!Form2!Field1.setfocus and that does not seem to shift the focus either.

    Form2 is a Popup if that makes any difference


    Wednesday, February 27, 2019 5:58 PM
  • I have moved the docmd.MoveSize code to Form2 and it is still not working...

    I apply MoveSize to Form2, then when the code returns to Form1 I run the code provided above and the Form1Top value is not equal to what I set it to.

    Is there a setting on Form2 that I should change? The odd part is that if I put in a static value of 4000 it seems to work.


    Wednesday, February 27, 2019 7:24 PM
  • For a popup form, the Top property is the distance to the top of the screen, while the Top property of an ordinary form is the distance to the ribbon in the Access window. So it's difficult to align a popup form and a non-popup form.

    Regards, Hans Vogelaar (

    Wednesday, February 27, 2019 7:57 PM