Kaja
Kaja

Reputation: 3057

Create a Resolution Independent Form in ms access

I have designed my form in 1280*1024 resulotion. They lookvery nice on my Monitor, but If I see on another Monitors, they look very chaotic . Is there a way that I solve this problem?

Upvotes: 1

Views: 4356

Answers (1)

user2140173
user2140173

Reputation:

It's not so easy. This is where MVC comes in very handy and you can distinguish between different components. You can have different views for different devices. Unfortunately VBA does not support that and you would have to implement your own framework to handle different screen resolutions.

The easiest way to avoid having to re-implement the design of your userform is to actually DESIGN it in your head before writing a single line of code. Think of the different resolutions(devices) your software is going to support, what the language you are using is supporting and what are your choices. Generally, think it over. In VBA I normally just go for the default size to avoid the headache of fitting someone's else screen.

You would have to redesign the entire UserForm. Not visually, but programmatically set both width, and height of the userform and make controls dependable on the current resolution. I do not recommend doing it this way but still this could be a solution.

You can achieve that by accessing the current resolution and modifying your Userform_Initialize() event.

So example, if the current resolution is 1024x768, you set the width and height to currentWidth-100px and currentHeight-100px.

If you open a new workbook and create an empty userform. Go to its code behind and add

Private Sub UserForm_Initialize()

    Me.Width = GetCurrent(0) - 600
    Me.Height = GetCurrent(1) - 800

End Sub

Then insert a module and add

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long

Sub A()

    UserForm1.Show
    Unload UserForm1

End Sub

Function GetCurrent(x As Long) As Long
    GetCurrent = GetSystemMetrics(x)
End Function

This will display a different size userform depending on the current resolution.

you can (but I wouldn't recommend it) use that technique. Note: depending on how many controls you have this may be the best approach but if you have lots of controls on the userform I would look for an alternative.


Alternatively, you can use the below code which checks the current screen resolution, warns the users and asks if the user wants to change his resolution.

The below code comes from here and the original author is DRJ

You stick the first part in the Workbook code behind

Option Explicit 

Private Sub Workbook_Open() 

    Call VerifyScreenResolution 

End Sub 

and the below part in a module

Option Explicit 

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long 
Const SM_CXSCREEN = 0 
Const SM_CYSCREEN = 1 

Sub VerifyScreenResolution(Optional Dummy As Integer) 

    Dim x  As Long 
    Dim y  As Long 
    Dim MyMessage As String 
    Dim MyResponse As VbMsgBoxResult 

    x = GetSystemMetrics(SM_CXSCREEN) 
    y = GetSystemMetrics(SM_CYSCREEN) 
    If x = 1024 And y = 768 Then 
    Else 
        MyMessage = "Your current screen resolution is " & x & " X " & y & vbCrLf & "This program " & _ 
        "was designed to run with a screen resolution of 1024 X 768 and may not function properly " & _ 
        "with your current settings." & vbCrLf & "Would you like to change your screen resolution?" 
        MyResponse = MsgBox(MyMessage, vbExclamation + vbYesNo, "Screen Resolution") 
    End If 
    If MyResponse = vbYes Then 
        Call Shell("rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,3") 
    End If 

End Sub 

update.

your initialize event is here

enter image description here

Upvotes: 1

Related Questions