Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-09-2019, 06:26 AM   #1
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Centering forms on any computer when maximized

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.

Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 06:32 AM   #2
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,429
Thanks: 68
Thanked 2,709 Times in 2,594 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Centering forms on any computer when maximized

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
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 09-09-2019, 06:46 AM   #3
theDBguy
Iím here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 4,698
Thanks: 50
Thanked 1,079 Times in 1,060 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: Centering forms on any computer when maximized

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?

__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is online now   Reply With Quote
Old 09-09-2019, 08:48 AM   #4
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Re: Centering forms on any computer when maximized

That was the first thing I tried. No success.
Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 08:49 AM   #5
theDBguy
Iím here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 4,698
Thanks: 50
Thanked 1,079 Times in 1,060 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: Centering forms on any computer when maximized

Quote:
Originally Posted by Lkwdmntr View Post
That was the first thing I tried. No success.
Sorry to hear that. I guess you'll have to use API then. Good luck!
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is online now   Reply With Quote
Old 09-09-2019, 08:53 AM   #6
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Re: Centering forms on any computer when maximized

Arnelgp,

I got an error, "Duplicate Option Statement"
Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 09:00 AM   #7
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,300
Thanks: 11
Thanked 716 Times in 666 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: Centering forms on any computer when maximized

Quote:
Originally Posted by Lkwdmntr View Post
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)>

__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
Old 09-09-2019, 09:22 AM   #8
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Re: Centering forms on any computer when maximized

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
Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 09:29 AM   #9
theDBguy
Iím here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 4,698
Thanks: 50
Thanked 1,079 Times in 1,060 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: Centering forms on any computer when maximized

Quote:
Originally Posted by Lkwdmntr View Post
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?
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is online now   Reply With Quote
Old 09-09-2019, 09:30 AM   #10
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,429
Thanks: 68
Thanked 2,709 Times in 2,594 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Centering forms on any computer when maximized

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.
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 09-09-2019, 10:32 AM   #11
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Re: Centering forms on any computer when maximized

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...b;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
Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 10:36 AM   #12
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,429
Thanks: 68
Thanked 2,709 Times in 2,594 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Centering forms on any computer when maximized

Private Sub Form_Load()

DoCmd.Maximize
Call CenterTheForm(Me)

End Sub
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 09-09-2019, 10:45 AM   #13
Lkwdmntr
Newly Registered User
 
Join Date: Jul 2019
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Lkwdmntr is on a distinguished road
Re: Centering forms on any computer when maximized

That is what I have.
Lkwdmntr is offline   Reply With Quote
Old 09-09-2019, 10:48 AM   #14
theDBguy
Iím here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 4,698
Thanks: 50
Thanked 1,079 Times in 1,060 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: Centering forms on any computer when maximized

Quote:
Originally Posted by Lkwdmntr View Post
That is what I have.
Hi. Did you see my question in post #9? I'm just curious and confused.
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is online now   Reply With Quote
Old 09-09-2019, 11:07 AM   #15
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,429
Thanks: 68
Thanked 2,709 Times in 2,594 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Centering forms on any computer when maximized

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

__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Centering forms on desktop Rob Ross Forms 2 06-13-2012 03:26 AM
Centering forms dcnick Modules & VBA 5 11-18-2010 09:58 AM
Centering controls on forms Howard Picken Forms 2 03-30-2003 02:54 PM
Keep forms maximized Gorio Forms 5 05-18-2001 09:10 AM
[SOLVED] Centering Photos on Reports and Forms DavidJason Reports 0 10-20-1999 06:27 AM




All times are GMT -8. The time now is 10:09 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World