none
Determine which monitor Microsoft Access opened in multi-monitor environment RRS feed

  • Question

  • I need to establish which monitor Microsoft Access 2010 has been opened on in a multi-monitor environment before any forms within the application are opened and find out what resolution that monitor has been set to.  I believe this requires API calls which I do not have much experience with.  My attempt to accomplish this always returns the resolution of the default monitor rather than the attached monitor that the application has been opened on.  Can someone suggest the VBA code that would accomplish this?
    Saturday, August 13, 2011 5:43 PM

Answers

  • Something to get you started ....
     
        Private Type RECT
            Left        As Long
            Top         As Long
            Right       As Long
            Bottom      As Long
        End Type
       
        Private Type typMonitorInfo
            cbSize      As Long
            rcMonitor   As RECT
            rcWork      As RECT
            dwFlags     As Long
        End Type
       
        Private Declare Function MonitorFromWindow _
                        Lib "User32.dll" _
                            (ByVal hWnd As Long, _
                             ByVal dwFlags As Long) _
                        As Long
       
        Private Declare Function GetMonitorInfo _
                        Lib "User32.dll" _
                        Alias "GetMonitorInfoW" _
                            (ByVal hMonitor As Long, _
                             ByVal lpMonitorInfo As Long) _
                        As Long
       
        Sub MonitorDetails()
       
            Dim hMonitor        As Long
            Dim MonitorInfo     As typMonitorInfo
            Dim apiReturnCode   As Long
           
            hMonitor = MonitorFromWindow(Application.hWndAccessApp, &H0&)
            MonitorInfo.cbSize = LenB(MonitorInfo)
           
            apiReturnCode = GetMonitorInfo(hMonitor, VarPtr(MonitorInfo))
           
            With MonitorInfo.rcMonitor
                Debug.Print "Horizontal resolution: " & .Right - ..Left
                Debug.Print "Vertical resolution: " & .Bottom - ..Top
            End With
       
        End Sub

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Rick49 Saturday, August 20, 2011 4:43 PM
    Thursday, August 18, 2011 10:15 AM

All replies

  • Hi Rick

    Rick49 wrote:

    I need to establish which monitor Microsoft Access 2010 has been opened
    on in a multi-monitor environment before any forms within the application
    are opened and find out what resolution that monitor has been set to. I
    believe this requires API calls which I do not have much experience with.
    My attempt to accomplish this always returns the resolution of the
    default monitor rather than the attached monitor that the application has
    been opened on. Can someone suggest the VBA code that would accomplish
    this?

    Maybe this helps? (I can't find there any information about Screen numbers or so, anyway)
    http://access.mvps.org/access/api/api0012.htm

    A few other API calls that may help are listed here:
    http://access.mvps.org/access/api/api0029.htm

    Henry

    Monday, August 15, 2011 4:36 AM
  • Hi Henry:

    Thanks for taking the time to respond.  I attempted to run the code in the first link and it always returns the resolution of the Primary monitor.  What I am attempting to do is to establish the resolution of a 23" monitor the application is opened on.  The reason:  If I attempt to open the application on a monitor with a resolution of 1920 dpi wide; Access hangs and must be forcefully aborted.  The form has code on the open event provided by Litwin/Getz/Gunderloy to resize the form.  If I comment this code out the form opens in the top left of the monitor; but not in the desired size.  As long as I keep the main Access application window to something less than 90% width of the monitor everything works as intended including the resizing code.  If I step the resolution of the monitor down one notch from 1920 everything works.  Seems to be some conflict between the Access form and the resolution of the monitor.  Have been attempting to resolve this issue for several months trolling the net without success.

    The second link you suggested deal with changing the user's monitor resolution.  I don't believe this to be wise since the user may be running a few applications on the 23" monitor as the same time.

    Rick


    Rick49
    Monday, August 15, 2011 3:42 PM
  • Hi Rick

    Sorry to not be able to help you on this. The best thing probably would be to go to a classic Visual Basic forum and ask the API freaks there what APIs would fulfill your needs. The declaration and code examples probably will work (if, with minor changes) also in VBA.

    Henry

    Tuesday, August 16, 2011 5:02 AM
  • Something to get you started ....
     
        Private Type RECT
            Left        As Long
            Top         As Long
            Right       As Long
            Bottom      As Long
        End Type
       
        Private Type typMonitorInfo
            cbSize      As Long
            rcMonitor   As RECT
            rcWork      As RECT
            dwFlags     As Long
        End Type
       
        Private Declare Function MonitorFromWindow _
                        Lib "User32.dll" _
                            (ByVal hWnd As Long, _
                             ByVal dwFlags As Long) _
                        As Long
       
        Private Declare Function GetMonitorInfo _
                        Lib "User32.dll" _
                        Alias "GetMonitorInfoW" _
                            (ByVal hMonitor As Long, _
                             ByVal lpMonitorInfo As Long) _
                        As Long
       
        Sub MonitorDetails()
       
            Dim hMonitor        As Long
            Dim MonitorInfo     As typMonitorInfo
            Dim apiReturnCode   As Long
           
            hMonitor = MonitorFromWindow(Application.hWndAccessApp, &H0&)
            MonitorInfo.cbSize = LenB(MonitorInfo)
           
            apiReturnCode = GetMonitorInfo(hMonitor, VarPtr(MonitorInfo))
           
            With MonitorInfo.rcMonitor
                Debug.Print "Horizontal resolution: " & .Right - ..Left
                Debug.Print "Vertical resolution: " & .Bottom - ..Top
            End With
       
        End Sub

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Rick49 Saturday, August 20, 2011 4:43 PM
    Thursday, August 18, 2011 10:15 AM
  • Hey Tony:

    Thanks for the code sample.  Works perfectly!

    Regards,

    Rick


    Rick49
    Saturday, August 20, 2011 4:44 PM
  • Hi,

    i'm having the same problem. Could you tell me where and how i use this code?

    Thanks

    Thursday, March 1, 2012 11:09 AM