'Class To handle resizing of all forms
Option Explicit
'>>>*******************************************************************
' Private Variables
'>>>*******************************************************************
Private mobjRSControls As ADODB.Recordset
'Private mbolSaveState As Boolean
'>>>*******************************************************************
' Private Properties
'>>>*******************************************************************
Private Property Get mbolSaveState() As Boolean
mbolSaveState = (mobjRSControls Is Nothing)
End Property
'>>>*******************************************************************
' Public Subs
'>>>*******************************************************************
Public Sub RestoreFormToOrig(ByRef objForm As Form)
Dim objControl As Control
If mobjRSControls Is Nothing Then
GoTo PROC_Exit
End If
With mobjRSControls
.Filter = adFilterNone
.Sort = ""
.Filter = "ControlName = '" & objForm.Name & "'"
If Not .BOF Then
.MoveFirst
End If
If Not .EOF Then
objForm.Height = .Fields("Height").Value
objForm.Width = .Fields("Width").Value
objForm.Font.SIZE = .Fields("Font").Value
End If
On Error Resume Next
For Each objControl In objForm
.Filter = adFilterNone
.Sort = ""
.Filter = "ControlName = '" & objControl.Name & "'"
If Not .BOF Then
.MoveFirst
End If
If Not .EOF Then
objControl.Font.SIZE = .Fields("Font").Value
objControl.Height = .Fields("Height").Value
objControl.Width = .Fields("Width").Value
objControl.Top = .Fields("Top").Value
objControl.Left = .Fields("Left").Value
End If
Next
End With
PROC_Exit:
Set objControl = Nothing
End Sub
Public Sub SaveOrigFormSize(ByRef objForm As Form)
On Error Resume Next
Dim objControl As Control
Set mobjRSControls = New ADODB.Recordset
With mobjRSControls
Set .ActiveConnection = Nothing
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
With .Fields 'Create the fields that will be apart of this recordset.
.Append "ControlName", adLongVarChar, 100
.Append "Height", adDouble
.Append "Width", adDouble
.Append "Left", adDouble
.Append "Top", adDouble
.Append "Font", adDouble
End With
.Open
'Start populating the recordset
.AddNew
.Fields("ControlName") = objForm.Name
.Fields("Height") = objForm.Height
.Fields("Width") = objForm.Width
.Fields("Font") = objForm.Font.SIZE
.UpdateBatch
For Each objControl In objForm.Controls
.AddNew
.Fields("ControlName") = objControl.Name
.Fields("Height") = objControl.Height
.Fields("Width") = objControl.Width
.Fields("Left") = objControl.Left
.Fields("Top") = objControl.Top
.Fields("Font") = objControl.Font.SIZE
.UpdateBatch
Next
End With
Set objControl = Nothing
End Sub
Public Sub ResizeFormBasedOnScale(ByVal sngSFX As Single, ByVal sngSFY As Single, ByRef objCurForm As Form)
On Error GoTo PROC_Err
Dim objControl As Control
Dim sngSFFont As Single
If mbolSaveState Then
SaveOrigFormSize objCurForm ', mcolControlPos
End If
'mbolSaveState = False
sngSFFont = (sngSFX + sngSFY) / 2 ' average scale
' Size the Controls for the new resolution
On Error Resume Next ' for read-only or nonexistent properties
With objCurForm
For Each objControl In .Controls 'i = 0 To .Count - 1
objControl.Left = objControl.Left * sngSFX
objControl.Top = objControl.Top * sngSFY
objControl.Width = objControl.Width * sngSFX
If TypeOf objControl Is ComboBox Then
' cannot change Height
Else
objControl.Height = objControl.Height * sngSFY
End If
'I think this code should be inside the above loop and I am moving the same.
' Be sure to resize and reposition before changing the FontSize
objControl.Font.SIZE = objControl.Font.SIZE * sngSFFont
Next
'If mbolRePosForm Then
' Now size the Form
'.Move .Left * sngSFX, .Top * sngSFY, .Width * sngSFX, .Height * sngSFY
.Move .Left, .Top, .Width * sngSFX, .Height * sngSFY
'End If
End With
PROC_Exit:
Set objControl = Nothing
Exit Sub
PROC_Err:
If Err.Number = 438 Then
Resume Next
Else
Resume PROC_Exit
End If
End Sub
'>>>*******************************************************************
' Private Subs
'>>>*******************************************************************
Private Sub Class_Terminate()
If Not mobjRSControls Is Nothing Then
If mobjRSControls.State <> adStateClosed Then
mobjRSControls.Close
End If
End If
Set mobjRSControls = Nothing
End Sub