Resizing a form based on screen resolution (1 Viewer)

Joosje

New member
Joined
Mar 18, 2010
Messages
7
I have a form that is pretty long (700) and I want it to re-size depending on the user's screen. I have found this code online but I don't think I'm using it right since it is not working for me. if someone has an easier way to do this or has any suggestions please share it. THANKSSS!!!

Attribute VB_Name = "FormControl"
Private List() As Control
Private curr_obj As Object
Private iHeight As Integer
Private iWidth As Integer
Private x_size As Double
Private y_size As Double

Private Type Control
Index As Integer
Name As String
Left As Integer
Top As Integer
width As Integer
height As Integer
End Type

Public Sub ResizeControls(frm As Form)
Dim i As Integer
' Get ratio of initial form size to current form size
x_size = frm.height / iHeight
y_size = frm.width / iWidth

'Loop though all the objects on the form
'Based on the upper bound of the # of controls
For i = 0 To UBound(List)
'Grad each control individually
For Each curr_obj In frm
'Check to make sure its the right control
If curr_obj.TabIndex = List(i).Index Then
'Then resize the control
With curr_obj
.Left = List(i).Left * y_size
.width = List(i).width * y_size
.height = List(i).height * x_size
.Top = List(i).Top * x_size
End With
End If
'Get the next control
Next curr_obj
Next i
End Sub

Public Function SetFontSize() As Integer
'Make sure x_size is greater than 0
If Int(x_size) > 0 Then
'Set the font size
SetFontSize = Int(x_size * 8)
End If
End Function

Public Sub GetLocation(frm As Form)
Dim i As Integer
' Load the current positions of each object into a user defined type array.
' This information will be used to rescale them in the Resize function.

'Loop through each control
For Each curr_obj In frm
'Resize the Array by 1, and preserve
'the original objects in the array
ReDim Preserve List(i)
With List(i)
.Name = curr_obj
.Index = curr_obj.TabIndex
.Left = curr_obj.Left
.Top = curr_obj.Top
.width = curr_obj.width
.height = curr_obj.height
End With
i = i + 1
Next curr_obj

' This is what the object sizes will be compared to on rescaling.
iHeight = frm.height
iWidth = frm.width
End Sub

Public Sub CenterForm(frm As Form)
frm.Move (Screen.width - frm.width) \ 2, (Screen.height - frm.height) \ 2
End Sub

Public Sub ResizeForm(frm As Form)
'Set the forms height
frm.height = Screen.height / 2
'Set the forms width
frm.width = Screen.width / 2
'Resize all of the controls
'based on the forms new size
ResizeControls frm
End Sub
 

Joosje

New member
Joined
Mar 18, 2010
Messages
7
My form is called "insertionform". I changed the name and I keep getting the following message: [invalid inside procedure] on this code:
Private Type Control
Index As Integer
Name As String
Left As Integer
Top As Integer
width As Integer
height As Integer
End Type

I guess I'm putting it under the wrong sub.
 

ghudson

Registered User
Joined
Jun 8, 2002
Messages
6,199
I do not think that code will work [as-is] in VBA for an access form. I think that was written for a VB form, not VBA. Can you get it to compile if you add this to the top of the module...

Code:
Option Compare Database
Option Explicit
That chunck of code at the top needs to go into a public module, not a form's module.

Where did you find the code?
 

ghudson

Registered User
Joined
Jun 8, 2002
Messages
6,199
I know there are other old threads discussing the same thing. Not sure if a "free" and working solution has been posted. Good luck!
 

Aiken004

New member
Joined
Sep 17, 2010
Messages
4
I program in 1024 by 768. If a 800 by 600 user opens my program, the program extends off the screen. if i apply your opinion will it help ???
 

gemma-the-husky

Super Moderator
Staff member
Joined
Sep 12, 2006
Messages
13,847
I think a type has to be defined in a public module, then you can declare a variable of type mytype in your sub.

try moving the type declaration to a module.

--------------
if you can find the "Access Cookbook", this has a demo application to resize forms and anchor certain controls to a particular placement within a form. so eg a command button would stay the same size, and move with the bottom right corner of a form - but a text box would grow so that the bottom remained a constant distance from the bottom of the form.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top