Positioning Form on Screen

The professor

Registered User.
Local time
Today, 16:11
Joined
Dec 5, 2012
Messages
14
Hi,

I have a modal, pop-up Form, that I'm using as a dialog box. But, when it opens, I want it positioned on screen in a certain place, but I'm struggling to work out how to do this. What properties do I need to alter/set, to achieve this?

Thanks in advance,
Prof.
 
A Form object has a Move method. If you handle the Load event of the form, you can run code there like . . .
Code:
Private Sub Form_Load()
   Me.Move 50, 50
End Sub
 
You can also set the top and left properties of the form

Me.Top = 3244
Me.Left = 1200
 
Inches of monitor doesn't correlate necessarily to pixels does it? So maybe the 15" and 17" are both 1080 pixels wide?

But I rarely move forms around like that. I usually set AutoCenter to true and leave it at that. For forms that I resize before they open (and are therefore not centered) I calculate how much I resize them, and then move them half that distance in the opposite direction of the resize.
 
Hi Lagbolt,

Thanks for that. This worked well for me, did the trick.
I had assumed (perhaps rather naively) that I would be able to set opening position in the Form Properties Sheet. Anyway, appreciated the help.
Prof.
 
But AccBlas, that is my point. You might be able to set your monitor to 1920x768, 1920x1050, 1920x1200 so moving to pixel locations or measured locations might no make sense.
 
In A 2007 I have had big troubles with pop up forms opened by using acDialog.
This forms unexpectedly auto resize and move on the screen.
By googling (is this an English word ?) I have found this code that center a form within Access window.
For me is enough to center, but the code can be easily adapted to place the form where you need.
I'll show you how I used the code and you will be able to adapt to your needs.

Open the pop up form named "Tevi" (Tubes, in English)
Code:
Private Sub cmdTevi_Click()
    DoCmd.OpenForm "Tevi", , , , , acDialog
End Sub
Open event for "Tevi"
Code:
Private Sub Form_Open(Cancel As Integer)
    Call FormMoveResize(Me)
End Sub
In a module:
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

'Mod apelare: 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

'All my pop up forms
Public Sub FormMoveResize(Frm As Form)
Dim W As Long 'Width (centimeters)
Dim H As Long 'High (centimeters)
    Select Case Frm.Name
        Case "Contoare_tipuri"
            W = 12: H = 14
        Case "Repartitoare_tipuri"
            W = 12: H = 14
        Case "Tevi"
            W = 11: H = 14
        Case "Kc_tevi"
            W = 12: H = 7
        Case "Kc_radiatoare"
            W = 12: H = 7
        Case "Ramificatii_SETuri_tevi"
            W = 13: H = 14
        Case "Ramificatii_SETuri_radiatoare"
            W = 13: H = 14
        Case "Radiatoare"
            W = 12: H = 14
        Case "Noduri_CI"
            W = 14: H = 16
        Case "Incinte_Denumiri"
            W = 9: H = 14
        Case "Incinte_CI_Tevi"
            W = 10: H = 14
        Case "Incinte_CI_radiatoare"
            W = 10: H = 14
        Case "Incinte_CD_tevi"
            W = 10: H = 14
        Case "Incinte_CD_radiatoare"
            W = 10: H = 14
        Case "Incinte_Radiatoare"
            W = 15: H = 14
        Case Else
            Debug.Print Frm.Name
            Stop
            Exit Sub
    End Select
    
    With Frm
        DoCmd.MoveSize , , Conv("Cm", "Twip", W), Conv("Cm", "Twip", H)
        Call gfncCenterForm(Frm)
    End With
    
End Sub

'Convert units. Actually this function convert only centimeters in twips
'FROMum = From measurement unit
'TOum = To measurement unit
FROMval = number of units

Public Function Conv(FROMum As String, TOum As String, FROMval)
    Select Case FROMum
        Case "Cm" 'Centimeter
            Select Case TOum
                Case "Twip" 'twips
                    Conv = FROMval * 566.929133858
                Case Else
                    Stop
                    Exit Function
            End Select
            
        Case Else
            Stop
            Exit Function
    End Select
End Function
That' all
Do NOT forget to set the Form AutoResize property to No.
I don't know if the AutoCenter property affect this code but, to be sure, also I have set this property to No.

Hope this is a help for you.

Cheers !
 

Users who are viewing this thread

Back
Top Bottom