Results 1 to 3 of 3
  1. #1
    Join Date
    May 2013

    Unanswered: Center across the screen

    How do you center a Form, Query or Report in the middle of the screen, I have looked in the property sheet but do not see any thing there, does this setting exist without VBA coding.
    I have done it in Design View but if someone has a different monitor size or screen resolution then it is going to be a mess.

  2. #2
    Join Date
    Nov 2011
    2 separate issues to deal with.To center your form/report just select AutoCenter. To deal with different screen resolutions you can use this code:
    'Module Declarations
    Global Const DesignResolutionX = 1024

    Global Const WM_HORZRES = 8
    Global Const WM_VERTRES = 10

    Dim Width As Integer
    Dim Factor As Single 'Used as multiplier for current size properties'

    Declare Function WM_apiGetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    Declare Function WM_apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
    Declare Function WM_apiGetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
    Declare Function WM_apiReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Declare Function WM_apiGetSystemMetrics Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

    Function GetScreenResolution() As String

    'returns the height and width
    Dim DisplayHeight As Integer
    Dim DisplayWidth As Integer
    Dim hDesktopWnd As Long
    Dim hDCcaps As Long
    Dim iRtn As Integer

    'API call get current resolution
    hDesktopWnd = WM_apiGetDesktopWindow() 'get handle to desktop
    hDCcaps = WM_apiGetDC(hDesktopWnd) 'get display context for desktop
    DisplayHeight = WM_apiGetDeviceCaps(hDCcaps, WM_VERTRES)
    DisplayWidth = WM_apiGetDeviceCaps(hDCcaps, WM_HORZRES)
    iRtn = WM_apiReleaseDC(hDesktopWnd, hDCcaps) 'release display context
    GetScreenResolution = DisplayWidth & "x" & DisplayHeight
    Width = DisplayWidth

    End Function
    ************************************************** **********
    Public Sub ReSizeForm(frm As Form)

    Dim ctl As Control
    'Dim i As Integer

    On Error Resume Next
    SetFactor 'Call to procedure SetFactor
    With frm
    .Width = frm.Width * Factor
    End With
    For Each ctl In frm.Controls
    With ctl
    .Height = ctl.Height * Factor
    .Left = ctl.Left * Factor
    .Top = ctl.Top * Factor
    .Width = ctl.Width * Factor
    .FontSize = .FontSize * Factor
    End With
    Next ctl

    End Sub

    Sub SetFactor()

    GetScreenResolution 'Call to function GetScreenResolution
    Factor = Width / DesignResolutionX

    End Sub

  3. #3
    Join Date
    May 2013
    I did try the AutoCenter but nothing changed.
    I have put your code into word and will study, as my knowledge of VBA is limited this will take me some time.
    My thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts