Opening form move to upper left automatically (1 Viewer)

Rashid

Member
Local time
Today, 02:40
Joined
Sep 6, 2019
Messages
30
I have created a ms access database for my friend. The login form when I open in my system opens with center position but when I open the same DB in my friends pc the login form opens upper left position. Even it is set auto center. I tried much to solve this problem but I don't succeed. Please give the solution for this. Thanks in advance.
 

MarkK

bit cruncher
Local time
Today, 02:40
Joined
Mar 17, 2004
Messages
8,186
1) I have had unpredictable form sizes before, but not locations. I can solve size problems if I open the form in design view, change the Auto Resize and Auto Center settings and save the form. Then open the form again in design view, and change those settings back.

2) There is an Access.Form.Move method, which allows you to place the form anywhere you want it.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:40
Joined
May 7, 2009
Messages
19,247
put in a Module:
Code:
'-----------------------------------------------------------------------------
' Utilities from http://blog.nkadesign.com/microsoft-access/
' (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

' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' '
' '   FROM form_Dialog
' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
'-----------------------------------------------------------------------------
' Get the screen resolution
'-----------------------------------------------------------------------------
Private Sub GetScreenResolution(ByRef Width As Long, ByRef Height As Long)
    Dim r As RECT
    Dim retVal As Long

#If VBA7 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

'-----------------------------------------------------------------------------
' 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 VBA7 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 VBA7 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


add code to the Load or Open event of the form:

Code:
Private Sub Form_Load()
   Call CenterForm(Me)
End Sub
 

Users who are viewing this thread

Top Bottom