Get screen resolution with VBA (1 Viewer)

yamus

Member
Local time
Today, 13:57
Joined
Aug 12, 2020
Messages
81
Hello
I was wondering how to get screen resolution with VBA
I found a solution on StackOverFlow

Declare Function GetSystemMetrics32 Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long

Sub ScreenRes()
Dim w As Long, h As Long
w = GetSystemMetrics32(0) ' width in points
h = GetSystemMetrics32(1) ' height in points
End Sub

But I get run-time error 453. Access can't find entry point GetSystemMetrics in User 32.
I am using Ms Access 2013 and Ms Access 2016
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 13:57
Joined
Jan 14, 2017
Messages
18,211
That should work providing the code is in a standard module with a different name to the function e.g. modResolution
If you have 64-bit Access then change the declaration to:
Code:
Declare PtrSafe Function GetSystemMetrics32 Lib "User32" _
Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:57
Joined
May 7, 2009
Messages
19,230
you need to convert it into a Function so it can return something:
Code:
#If VBA7 Then
    Declare PtrSafe Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#Else
    Declare Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#End If
Function ScreenRes() As String
    Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' width in points
    h = GetSystemMetrics32(1) ' height in points
    ScreenRes = w & "x" & h
End Function
 

isladogs

MVP / VIP
Local time
Today, 13:57
Joined
Jan 14, 2017
Messages
18,211
Whilst using PtrSafe will work perfectly in 32-bit VBA7, so will the version in post #1.
If all users are on 32/64bit A2013/2016, conditional compilation isn't needed. Just use the PtrSafe version of the API
 

yamus

Member
Local time
Today, 13:57
Joined
Aug 12, 2020
Messages
81
That should work providing the code is in a standard module with a different name to the function e.g. modResolution
If you have 64-bit Access then change the declaration to:
Code:
Declare PtrSafe Function GetSystemMetrics32 Lib "User32" _
Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Hello
Thanks for the help
It worked very well
Again, I appreciate your help
 

yamus

Member
Local time
Today, 13:57
Joined
Aug 12, 2020
Messages
81
you need to convert it into a Function so it can return something:
Code:
#If VBA7 Then
    Declare PtrSafe Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#Else
    Declare Function GetSystemMetrics32 Lib "User32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
#End If
Function ScreenRes() As String
    Dim w As Long, h As Long
    w = GetSystemMetrics32(0) ' width in points
    h = GetSystemMetrics32(1) ' height in points
    ScreenRes = w & "x" & h
End Function
Hello
I am very grateful for your help sir. Thanks a lot
 

Users who are viewing this thread

Top Bottom