Getting form position.

redalert

Registered User.
Local time
Today, 03:24
Joined
Oct 7, 2013
Messages
62
Does anybody know why the following code:

Code:
  MsgBox Me.Top

returns an error: Method or data member not found (Error 461)

I want to ascertain the position of the current form so that I can position it where I want to on the screen.

Thanks
 
Try the below, (not tested).
Code:
MsgBox (Me.Form.WindowTop)
 
Adapt the following code to your needs:
Code:
Option Compare Database
Option Explicit

' API declarations:
Private Declare Function apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Long, lpRect As typRect) As Long
Private Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As typRect) As Long
Private Declare Function apiSetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

' Type declarations:
Private Type typRect
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

' Constant declarations:
Private Const SW_RESTORE = 9
Private Const SWP_NOSIZE = &H1 ' Don't alter the size
Private Const SWP_NOZORDER = &H4 ' Don't change the Z-order
Private Const SWP_SHOWWINDOW = &H40 ' Display the window

' **************************************
' * Center a form in the Access window *
' **************************************
' Created 1-22-2002 by Peter M. Schroeder

'How to apply: Call gfncCenterForm(Me)

Public Function gfncCenterForm(parForm As Form) As Boolean
    Dim varAccess As typRect, varForm As typRect
    Dim varX As Long, varY As Long
    
    On Error GoTo CenterForm_Error
    Call apiGetClientRect(hWndAccessApp, varAccess) ' Get the Access client area coordinate
    Call apiGetWindowRect(parForm.hwnd, varForm) ' Get the form window coordinates
    varX = CLng((varAccess.Left + varAccess.Right) / 2) - CLng((varForm.Right - varForm.Left) / 2) ' Calculate a new left for the form
    varY = CLng((varAccess.Top + varAccess.Bottom) / 2) - CLng((varForm.Bottom - varForm.Top) / 2) ' Calculate a new top for the form
    varY = varY - 45 ' Adjust top for true center
    varY = varY - 20 ' Adjust top for appearance
    Call apiShowWindow(parForm.hwnd, SW_RESTORE) ' Restore form window
    Call apiSetWindowPos(parForm.hwnd, 0, varX, varY, (varForm.Right - varForm.Left), (varForm.Bottom - varForm.Top), SWP_NOZORDER Or SWP_SHOWWINDOW Or SWP_NOSIZE) ' Set new form coordinates
    gfncCenterForm = True
Exit Function
    
CenterForm_Error:
    gfncCenterForm = False
End Function
 
JHB's aircode will work, although it can be simplified to

Me.WindowTop

or even simpler, just

WindowTop

Note that the figure returned is in Twips:

1440 Twips = 1 inch

567 Twips = 1 centimeter

The Top, used in the OP's code, is the Property of a Control on a Form or Report, in relation to the top of the Form.

So, ControlName.Top would return, in twips, the distance between the top of ControlName and the top of the Form.

Linq ;0)>
 
I want to ascertain the position of the current form so that I can position it where I want to on the screen.

I think it should be asked why it is necessary to know where the Form is in order to position it? If the Form is sometimes already at the exact X/Y pixel then maybe it doesn't need to be moved but how often would that be the case?

Using the Me.Move method will not position the Form 'on the screen' as requested. It will position the Form relative the Access window and they are not the same thing. To position the Form on the screen the API call will be necessary.

http://allapi.mentalis.org/apilist/SetWindowPos.shtml

Code:
Public Sub MoveTo(ByRef frm As Form, ByVal lngLeftPixel As Long, ByVal lngTopPixel As Long)

    SetWindowPos frm.hWnd, 0, lngLeftPixel, lngTopPixel, 0, 0, SWP_NOZORDER Or SWP_SHOWWINDOW Or SWP_NOSIZE

End Sub

Chris.
 

Users who are viewing this thread

Back
Top Bottom