Form open from another Form (1 Viewer)

DirtyDave

New member
Local time
Today, 00:51
Joined
Jan 4, 2019
Messages
1
Hello my freinds,
I have a problem, what i cant fix.I have 2 monitor, when i try to open a form from a form on my second monitor it has a runtime error 6 overflow if i try to open on my secondary monitors's farest half. Its only work on 2/3 of my monitors.
Sorry for bad english :(

www kepkuldes.com/images/9e8fa21fb0d69fc7bebcd0e973f14f25.png
www kepkuldes.com/images/869dfc51921e66507aecc67ab6e6aca4.png
www kepkuldes.com/images/a041c52b56c56378b9073b5de6fe0877.png


I tried this one:

Dim strFormToOpen As String
Dim ctlTarget As Access.Control
Dim lngBorderHoriz As Long
Dim lngBorderVert As Long

strFormToOpen = "Form2" 'Form3 megnyitása

Set ctlTarget = Me.txtMyTextBox 'textbox neve

DoCmd.OpenForm strFormToOpen

lngBorderHoriz = (Me.WindowWidth - Me.InsideWidth) / 2
lngBorderVert = (Me.WindowHeight - Me.InsideHeight) / 2

With Forms(strFormToOpen)
.Move _
(Me.WindowLeft - .WindowWidth) + lngBorderHoriz + ctlTarget.Left, _
Me.WindowTop + lngBorderVert + ctlTarget.Top
End With
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:51
Joined
May 7, 2009
Messages
19,233
i have not tried this coz i dont have dual monitor.
give it a test, create a Class Module (clsExtendedDisplay) and paste
the code:
Code:
' https://www.ozgrid.com/forum/forum/archived-forums/technical-issues-help/29950-userforms-with-multiple-monitors
 'Function to get screen resolution
#If VBA7 Then
Private Declare PtrSafe Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#Else
Private Declare Function GetSystemMetrics32 Lib "user32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#End If
 '======================================================================================
 '======================================================================================
 '==============This object is used to return positioning information on a sytem with
 '==============one extended display. My primary screen is the left one, I have not
 '==============tried it with another set up.
 '======================================================================================
 '======================================================================================
 'Notes
 
    'x = 0   'This is width of primary display
    'x = 1   'This is height of primary display
    'x = 78  'Total width of both displays
    'ValueYouWant = GetSystemMetrics(x)


'======================================================================================
'This portion found at http://www.ozgrid.com/forum/showthread.php?t=198442
'======================================================================================
 'Functions to get DPI
 #If VBA7 Then
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
#Else
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
#End If
Private Const LOGPIXELSX = 88 'Pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72 'A point is defined as 1/72 inches
 
Private blnMainScreen       As Boolean
Private blnMultipleScreens  As Boolean
Private blnSetUp            As Boolean  'True after being set up
Public frmPairedForm        As Object




 'Return DPI
Private Function PointsPerPixel() As Double


    Dim hDC As Long
    Dim lDotsPerInch As Long
     
    hDC = GetDC(0)
    lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
    PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
    ReleaseDC 0, hDC
    
End Function
'======================================================================================


'=================================================================
'Return the right position of extended display
'=================================================================
Public Sub CenterFormOnSecondaryScreen()
    
    Dim dblScreen1Width As Double                               'Primary display width
    Dim dblTotalWidth   As Double                               'Total width of both screens
    Dim dblScreen2Left  As Double                               'Left of second screen
    
    Me.SetUp
    
    dblScreen1Width = GetSystemMetrics32(0) * PointsPerPixel    'Get it
    
    dblTotalWidth = GetSystemMetrics32(78) * PointsPerPixel     'Get it


    dblScreen2Left = dblTotalWidth - dblScreen1Width            'Get middle of displays
    
    frmPairedForm.Left = ((dblScreen2Left) + ((dblTotalWidth - dblScreen2Left) / 2)) - (frmPairedForm.Width / 2)
    
End Sub


'=================================================================
'Position the passed user form reference in center screen of primary display
'=================================================================
Public Sub CenterFormOnPrimaryScreen()
    
    Dim dblScreen1Width As Double                                       'Primary display width
    Dim dblTotalWidth   As Double                                       'Total width of both screens
    Dim dblScreen2Left  As Double
    
    Me.SetUp
    
    dblScreen1Width = GetSystemMetrics32(0) * PointsPerPixel            'Get it
    
    dblTotalWidth = GetSystemMetrics32(78) * PointsPerPixel             'Get it


    dblScreen2Left = dblTotalWidth - dblScreen1Width                    'Get middle of displays


    frmPairedForm.Left = (dblScreen2Left / 2) - (frmPairedForm.Width / 2)


End Sub


'=======================================================================
'Figure out whether or not to make changes based on if the user has multiple screens or not
'=======================================================================
Public Sub SetUp()
    
    Dim dblScreen1Width As Double                               'For width of main screen
    Dim dblTotalWidth   As Double
    
    dblTotalWidth = GetSystemMetrics32(78) * PointsPerPixel     'Get it
    dblScreen1Width = GetSystemMetrics32(0) * PointsPerPixel    'Get it
    
    If Not dblScreen1Width = dblTotalWidth Then                 'If primary display width <> total width then
    
        blnMultipleScreens = True                               'You have multiple displays
    
    Else
        
        blnMultipleScreens = False                              'You do not have multiple displays
    
    End If
    
    If frmPairedForm.Left < dblScreen1Width Then                'If form start up left pos is on the primary display then
        
        blnMainScreen = True                                    'Userform is on the main screen
    
    Else
    
        blnMainScreen = False
    
    End If
    
End Sub


'=======================================================================
'If on the main screen, and there are multiple displays, move to secondary. Vice Versa
'=======================================================================
Public Sub SwapScreens()
    
    Me.SetUp                                'If this hasn't been set up yet, then do it
    
    If blnMultipleScreens Then              'Only continue if the user has multiple screens
        
        If blnMainScreen Then               'If on primary display
        
            Me.CenterFormOnSecondaryScreen  'Move to secondary
        
        Else                                'Else
        
            Me.CenterFormOnPrimaryScreen    'Move to primary
            
        End If


    End If
        
End Sub


''''''''''''
Public Sub Init(frm As Form)
Set frmPairedForm = frm
End Sub

Dim ED as clsExtendedDisplay
open your second form (Docmd.OpenForm...)
set ED = New clsExtendedDisplay
ED.Init Forms!theNameOfSecondForm
ED.Swap
 

isladogs

MVP / VIP
Local time
Today, 08:51
Joined
Jan 14, 2017
Messages
18,216
Just to say I've tried the code arnel obtained from ozgrid.com.
The final line should I believe be ED.SwapScreens

The code looks like it ought to work but fails on that line with error 2465

I've also looked at the original URL & found slightly different code but my tweaks have all been unsuccessful. Perhaps i'm missing something obvious

Will try a different approach later if I have time

EDIT
Forgot to say that there is of course a much easier solution that involves no code.
Move each form to the screen you want it to be on then close it.
Next time you open the form it will open in the same place.
The only problem with that is users with only one monitor will have issues.
 
Last edited:

Users who are viewing this thread

Top Bottom