Centering forms on any computer when maximized (1 Viewer)

Lkwdmntr

Registered User.
Local time
Today, 08:17
Joined
Jul 10, 2019
Messages
277
Hello again,

I was wondering if anyone had a handy trick that would center all my forms on any computer I run my database file from. I have them pretty much centered, just my eyeing them, but it would be nice to be exact on any computer used.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:17
Joined
May 7, 2009
Messages
19,175
add code to the Open or Load event your Forms:
Code:
Call CenterForm(Me)
paste the following code in Standard Module.
Code:
'-----------------------------------------------------------------------------
' (c) Renaud Bompuis, 2008
' Licensed under the Creative Commons Attribution License
' http://creativecommons.org/licenses/by/3.0/
' http://creativecommons.org/licenses/by/3.0/legalcode
'
' Free for re-use in any application or tutorial providing clear credit
' is made about the origin of the code and a link to the site above
' is prominently displayed where end-user can access it.
'
' updated by arnel gp for 64bit
'-----------------------------------------------------------------------------
Option Compare Database
Option Explicit


Private Type RECT
    X1 As Long
    Y1 As Long
    X2 As Long
    Y2 As Long
End Type
#If VBA7 Then
    Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hWnd As LongPtr, lpRect As RECT) As Long
    Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
    Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, ByVal hdc As LongPtr) As Long
    Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, ByVal nIndex As Long) As Long
#Else
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, Rectangle As RECT) As Boolean
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
#End If
Private Const WU_LOGPIXELSX = 88
Private Const WU_LOGPIXELSY = 90

Sub CenterForm(f As Form)
    Dim formWidth As Long, formHeight As Long
    Dim maxWidth As Long, maxHeight As Long
    Dim ScreenWidth As Long, ScreenHeight As Long
    Dim formAllMarginsHeight As Long, formAllMarginsWidth As Long

    ' Compute maximal acceptable dialog box size in twips
    GetScreenResolution ScreenWidth, ScreenHeight
    ScreenWidth = ConvertPixelsToTwips(ScreenWidth, 0)
    ScreenHeight = ConvertPixelsToTwips(ScreenHeight, 0)
    maxWidth = ScreenWidth * 0.6
    maxHeight = ScreenHeight * 0.9

    ' Calculate the height and width of the area around the textbox
    formAllMarginsHeight = f.WindowHeight - f.Section(acDetail).Height
    formAllMarginsWidth = f.Width

    ' Assess proper width and height of the overall dialog box
    formWidth = formAllMarginsWidth
    formHeight = formAllMarginsHeight

    ' Adjust position of the th box to the middle if there is not much text.
    If formHeight < f.WindowHeight Then
        formHeight = f.WindowHeight
    End If

    ' Redimension the dialog and display the message at the center of the screen
    DoCmd.MoveSize (ScreenWidth - formWidth) / 2, (ScreenHeight - formHeight) / 2, formWidth, formHeight

End Sub


'-----------------------------------------------------------------------------
' Pixel to Twips conversions
'-----------------------------------------------------------------------------
' cf http://support.microsoft.com/default.aspx?scid=kb;en-us;210590
' To call this function, pass the number of twips you want to convert,
' and another parameter indicating the horizontal or vertical measurement
' (0 for horizontal, non-zero for vertical). The following is a sample call:
'

Function ConvertTwipsToPixels(lngTwips As Long, lngDirection As Long) As Long
'Handle to device
    Dim lngPixelsPerInch As Long
    Const nTwipsPerInch = 1440

#If Win64 Then
    Dim lngDC As LongPtr
#Else
    Dim lngDC As Long
#End If
    
    lngDC = GetDC(0)
    If (lngDirection = 0) Then       'Horizontal
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
    Else                            'Vertical
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
    End If
    lngDC = ReleaseDC(0, lngDC)
    ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
End Function

Function ConvertPixelsToTwips(lngPixels As Long, lngDirection As Long) As Long
'Handle to device
    Dim lngPixelsPerInch As Long
    Const nTwipsPerInch = 1440
    
#If Win64 Then
    Dim lngDC As LongPtr
#Else
    Dim lngDC As Long
#End If
    
    lngDC = GetDC(0)

    If (lngDirection = 0) Then       'Horizontal
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
    Else                            'Vertical
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
    End If
    lngDC = ReleaseDC(0, lngDC)
    ConvertPixelsToTwips = (lngPixels * nTwipsPerInch) / lngPixelsPerInch
End Function
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:17
Joined
Oct 29, 2018
Messages
21,359
Hi. To center a form on the screen, it must mean you're using popup forms, right? If so, have you tried simply setting the Auto Center property to Yes?
 

Lkwdmntr

Registered User.
Local time
Today, 08:17
Joined
Jul 10, 2019
Messages
277
That was the first thing I tried. No success.
 

Lkwdmntr

Registered User.
Local time
Today, 08:17
Joined
Jul 10, 2019
Messages
277
Arnelgp,

I got an error, "Duplicate Option Statement"
 

missinglinq

AWF VIP
Local time
Today, 11:17
Joined
Jun 20, 2003
Messages
6,423
Arnelgp,

I got an error, "Duplicate Option Statement"

When you open a Standard Module, it'll already have an Option Statement at the very top of it...simply go to the very top of the module (above the code you pasted in) and delete this.

Linq ;0)>
 

Lkwdmntr

Registered User.
Local time
Today, 08:17
Joined
Jul 10, 2019
Messages
277
Sorry, I should have been able to figure that one out. I didn't see the other line on the top. Anyway, after I fixed that, I got another error "Expected variable or procedure, not module" I pasted this in my OnLoad event.

Private Sub Form_Load()

DoCmd.Maximize
Call CenterForm(Me)

End Sub​
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:17
Joined
Oct 29, 2018
Messages
21,359
Sorry, I should have been able to figure that one out. I didn't see the other line on the top. Anyway, after I fixed that, I got another error "Expected variable or procedure, not module" I pasted this in my OnLoad event.
Private Sub Form_Load()

DoCmd.Maximize
Call CenterForm(Me)

End Sub​
Interesting... Are you saying when you Maximize a form it doesn't occupy the entire screen, so it needs to be centered?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:17
Joined
May 7, 2009
Messages
19,175
the error is caused when your Module name is same as the function/sub name.
rename the sub to anything (eg. CenterTheForm).
use the new sub name to the load event of the form.
 

Lkwdmntr

Registered User.
Local time
Today, 08:17
Joined
Jul 10, 2019
Messages
277
Now it says "Sub or Function not defined"

I changed the code and the Module name is CenterForm

'-----------------------------------------------------------------------------
' (c) Renaud Bompuis, 2008
' Licensed under the Creative Commons Attribution License
' http://creativecommons.org/licenses/by/3.0/
' http://creativecommons.org/licenses/by/3.0/legalcode
'
' Free for re-use in any application or tutorial providing clear credit
' is made about the origin of the code and a link to the site above
' is prominently displayed where end-user can access it.
'
' updated by arnel gp for 64bit
'-----------------------------------------------------------------------------
Option Compare Database
Option Explicit


Private Type RECT
X1 As Long
Y1 As Long
X2 As Long
Y2 As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDC As LongPtr) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long
#Else
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, Rectangle As RECT) As Boolean
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
#End If
Private Const WU_LOGPIXELSX = 88
Private Const WU_LOGPIXELSY = 90

Sub CenterTheForm(f As Form)
Dim formWidth As Long, formHeight As Long
Dim maxWidth As Long, maxHeight As Long
Dim ScreenWidth As Long, ScreenHeight As Long
Dim formAllMarginsHeight As Long, formAllMarginsWidth As Long

' Compute maximal acceptable dialog box size in twips
GetScreenResolution ScreenWidth, ScreenHeight
ScreenWidth = ConvertPixelsToTwips(ScreenWidth, 0)
ScreenHeight = ConvertPixelsToTwips(ScreenHeight, 0)
maxWidth = ScreenWidth * 0.6
maxHeight = ScreenHeight * 0.9

' Calculate the height and width of the area around the textbox
formAllMarginsHeight = f.WindowHeight - f.Section(acDetail).Height
formAllMarginsWidth = f.Width

' Assess proper width and height of the overall dialog box
formWidth = formAllMarginsWidth
formHeight = formAllMarginsHeight

' Adjust position of the th box to the middle if there is not much text.
If formHeight < f.WindowHeight Then
formHeight = f.WindowHeight
End If

' Redimension the dialog and display the message at the center of the screen
DoCmd.MoveSize (ScreenWidth - formWidth) / 2, (ScreenHeight - formHeight) / 2, formWidth, formHeight

End Sub


'-----------------------------------------------------------------------------
' Pixel to Twips conversions
'-----------------------------------------------------------------------------
' cf http://support.microsoft.com/default.aspx?scid=kb;en-us;210590
' To call this function, pass the number of twips you want to convert,
' and another parameter indicating the horizontal or vertical measurement
' (0 for horizontal, non-zero for vertical). The following is a sample call:
'

Function ConvertTwipsToPixels(lngTwips As Long, lngDirection As Long) As Long
'Handle to device
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440

#If Win64 Then
Dim lngDC As LongPtr
#Else
Dim lngDC As Long
#End If

lngDC = GetDC(0)
If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch
End Function

Function ConvertPixelsToTwips(lngPixels As Long, lngDirection As Long) As Long
'Handle to device
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440

#If Win64 Then
Dim lngDC As LongPtr
#Else
Dim lngDC As Long
#End If

lngDC = GetDC(0)

If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertPixelsToTwips = (lngPixels * nTwipsPerInch) / lngPixelsPerInch
End Function​
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:17
Joined
May 7, 2009
Messages
19,175
Private Sub Form_Load()

DoCmd.Maximize
Call CenterTheForm(Me)

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:17
Joined
May 7, 2009
Messages
19,175
i missed one sub, add this to the end of your code:
Code:
Private Sub GetScreenResolution(ByRef Width As Long, ByRef Height As Long)
    Dim r As RECT
    Dim RetVal As Long

#If Win64 Then
    Dim hWnd As LongPtr
#Else
    Dim hWnd As Long
#End If
    hWnd = GetDesktopWindow()
    RetVal = GetWindowRect(hWnd, r)
    Width = r.X2 - r.X1
    Height = r.Y2 - r.Y1
End Sub
 

isladogs

MVP / VIP
Local time
Today, 15:17
Joined
Jan 14, 2017
Messages
18,186
I found 3 different sets of code in my code library used to centre a form on the screen. So I thought it might be interesting to create an example app comparing each of those approaches together with the code arnelgp supplied.

Form1 - uses code from https://answers.microsoft.com/en-us/msoffice/forum/all/center-a-form-using-vba/c834f2f5-a03b-4c52-b4ae-e891606ab9af

Form2 - uses class module code from http://access.mvps.org/access/forms/frm0042.htm

Form3 - uses code from the next version (v7.5) of my Accurately Move Forms & Controls example app

Form4 - uses code by Renaud Bompuis as supplied by arnelgp in an earlier post

In order to compare like with like, each is a borderless popup form and all are the same size.
If you open each of the forms in turn, you will notice that the different versions give slightly different positions for the screen centre..
Forms 3 & 4 seem to give identical results but, to my eyes, Form2 gives the 'real centre'

You can hold the left mouse button down on the form header to drag to a new position then re-centre the forms with a button click

EDIT
Forgot to say that if the navigation pane or ribbon are maximised/ minimised/hidden whilst the forms are open, Form2 (based on the class module code) is repositioned but the others say in the same place.

EDIT 2:
OOPS - Form2 not a popup. The rest are. See updated versions in post #20
 

Attachments

  • CentreFormExamples.zip
    79.9 KB · Views: 57
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 08:17
Joined
Oct 29, 2018
Messages
21,359
Please pardon me, but am I the only one confused here? How can the code the OP provided include the line:
Code:
DoCmd.Maximize
and then expect us to provide a code to center this form on the screen? Please help me understand the question. Thank you.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:17
Joined
Feb 19, 2002
Messages
42,986
I think the user is asking for his form elements to be centered rather than centering a pop up form window. When you maximize forms (which I personally dislike), all the controls appear in the upper left corner of the screen. Newer versions of Access have anchoring features that allow you to control this better so that the controls can expand to fill the visual space or you can use Peter's Shrinker/Stretcher code for less than $40 (I think)
 

isladogs

MVP / VIP
Local time
Today, 15:17
Joined
Jan 14, 2017
Messages
18,186
I agree.
It doesn't make sense and the OP has failed to respond to your earlier question'

Using tabbed documents, the form is always maximised
Using overlapping windows and DoCmd.Maximize has the same result.
In neither case does the form need centering

NOTE:
1. I believe setting auto center = Yes only centres the form horizontally on screen. It doesn't do the same vertically which is why there are so many different versions of the centering code online
2. For both form & control resizing/repositioning, you can also use the free code originally by Jamie Czernak that I have updated over the years. See http://www.mendipdatasystems.co.uk/automatic-form-resizing-1/4594554784
In my opinion, its much better than anchoring and at least as good as Shrinker/Stretcher ...plus its free
 

isladogs

MVP / VIP
Local time
Today, 15:17
Joined
Jan 14, 2017
Messages
18,186
In case anyone is interested / cares, I've updated the utility from post #16 as I realised one of the forms wasn't a popup whilst the others were

Two amended versions attached
v1 - all forms NOT popup - so are centred on the application window. Maximising/minimising/hiding ribbon or navigation pane causes all forms to be repositioned
v2 - all forms are popup so are 'centred' on the screen and are unaffected by changes to the navigation pane / ribbon

As before the different versions of the code give slightly different 'centre positions' for forms 1, 2 & 3 with form 4 identical to form 3.
Choose whichever you think looks best or works best with your application
 

Attachments

  • CentreFormExamples_v1_NotPopup.zip
    84.5 KB · Views: 55
  • CentreFormExamples_v2_Popup.zip
    81.8 KB · Views: 66

Users who are viewing this thread

Top Bottom