So. I started a project awhile back and for the last couple of years I've been building my "toolbox" of code. With this current project, I started with one form, then added another, and now I'm at three. With the first two, I wrote 2 subs to call on resize in which the objects are centered. An Example of that is as follows:
Code:
Option Compare Database
Option Explicit
Dim winWt, winHt As Integer
Sub AdjustDash() 'Dashboard
Dim mainWidth, mainHeight As Integer
winWt = Forms!frm_02_Dashboard.InsideWidth
winHt = Forms!frm_02_Dashboard.InsideHeight
mainWidth = Forms!frm_02_Dashboard!Box1.Width
mainHeight = 7500 '5.2 Inches coverted to twips
If (winWt > mainWidth) And (winHt > mainHeight) Then
Forms!frm_02_Dashboard.ScrollBars = 0
Call CenterHz((mainWidth))
Call CenterVt((mainHeight))
ElseIf (winWt < mainWidth) And (winHt > mainHeight) Then
Forms!frm_02_Dashboard.ScrollBars = 1
Call CenterVt((mainHeight))
ElseIf (winWt > mainWidth) And (winHt < mainHeight) Then
Forms!frm_02_Dashboard.ScrollBars = 2
Call CenterHz((mainWidth))
Else
Forms!frm_02_Dashboard.ScrollBars = 3
End If
End Sub
Private Sub CenterHz(theWidth As Integer)
Dim StartLeft As Integer
StartLeft = (winWt - theWidth) / 2
Forms!frm_02_Dashboard!lblRange.Left = StartLeft + 120
Forms!frm_02_Dashboard!imgGRIcon.Left = StartLeft + 2460
Forms!frm_02_Dashboard!Box1.Left = StartLeft
Forms!frm_02_Dashboard!Box2.Left = StartLeft
Forms!frm_02_Dashboard!Box3.Left = StartLeft
Forms!frm_02_Dashboard!lblDelay.Left = StartLeft + 2460
Forms!frm_02_Dashboard!lblShift.Left = (StartLeft + 2460)
Forms!frm_02_Dashboard!cmdLogRange.Left = (StartLeft + 8700)
Forms!frm_02_Dashboard!lblLastStops.Left = (120 + StartLeft)
Forms!frm_02_Dashboard!lblDTG.Left = (120 + StartLeft)
Forms!frm_02_Dashboard!lblLastShifts.Left = (120 + StartLeft)
Forms!frm_02_Dashboard!lblLastShift.Left = (120 + StartLeft)
End Sub
Private Sub CenterVt(theHeight As Integer)
Dim StartTop As Integer
StartTop = (winHt - theHeight) / 3
Forms!frm_02_Dashboard!lblRange.Top = (StartTop + 60) '60 is distance from top
Forms!frm_02_Dashboard!imgGRIcon.Top = StartTop + 1080
Forms!frm_02_Dashboard!Box1.Top = (StartTop)
Forms!frm_02_Dashboard!Box2.Top = (StartTop + 2760)
Forms!frm_02_Dashboard!Box3.Top = (StartTop + 5520)
Forms!frm_02_Dashboard!lblDelay.Top = StartTop + 2160
Forms!frm_02_Dashboard!lblShift.Top = (StartTop + 60)
Forms!frm_02_Dashboard!cmdLogRange.Top = (StartTop + 60)
Forms!frm_02_Dashboard!lblLastStops.Top = (StartTop + 2820)
Forms!frm_02_Dashboard!lblDTG.Top = (StartTop + 3270)
Forms!frm_02_Dashboard!lblLastShifts.Top = (StartTop + 5580)
Forms!frm_02_Dashboard!lblLastShift.Top = (StartTop + 6030)
End Sub
I wrote a similar module for the second form for centering. Now that I have a third form and have a possibility of more, I don't want to continue to write a module for each and with each module, I don't want to list the objects/controls w/ distance from left and top... as it is very time consuming. With the 2 forms that do have the resize module, I can move the access window around and resize it and the objects will move to the center of the window as intended.
So the rewrite:
Code:
Option Compare DatabaseOption Explicit
Dim winWt, winHt As Integer
Public Sub colCtrlReq(frm As Form)
Dim nWidth, nHeight, TPI, StartLeft, StartTop As Integer
TPI = 1440 'Twips Per Inch
nWidth = TPI * 7.5 'Original Width of Form
nHeight = TPI * 5.2076 'Original Height of Form
winWt = frm.InsideWidth
winHt = frm.InsideHeight
If (winWt > nWidth) And (winHt > nHeight) Then
'No Scroll bars
frm.ScrollBars = 0
'Adjust Both Vertical and Horizontal Placement of objects
Call AdjustHz(frm, (nWidth))
Call AdjustVt(frm, (nHeight))
ElseIf (winWt < nWidth) And (winHt > nHeight) Then
'Only Horizontal Scroll bar added
frm.ScrollBars = 1
StartTop = (winHt - nHeight) / 3
'Only Vertical Adjusted
Call AdjustVt(frm, (nHeight))
ElseIf (winWt > nWidth) And (winHt < nHeight) Then
'Only Vertical Scroll bar added
frm.ScrollBars = 2
'Only Horizontal Adjusted
Call AdjustHz(frm, (nWidth))
Else
'Both Scroll bars added
frm.ScrollBars = 3
'No adjustments made to objects
End If
End Sub
Private Sub AdjustHz(frm As Form, theWidth As Integer)
Dim StartLeft As Integer
Dim ctl As Control
StartLeft = (winWt - theWidth) / 2
For Each ctl In frm
If (ctl.ControlType > 99) And (ctl.ControlType < 115) Then
'MsgBox ctl.ControlType & ": " & ctl.Name -> cmd to tblsht
ctl.Left = StartLeft + ctl.Left
End If
Next ctl
Set ctl = Nothing
End Sub
Private Sub AdjustVt(frm As Form, theHeight As Integer)
Dim StartTop As Integer
Dim ctl As Control
StartTop = (winHt - theHeight) / 3
For Each ctl In frm
If (ctl.ControlType > 99) And (ctl.ControlType < 115) Then
ctl.Top = StartTop + ctl.Top
End If
Next ctl
Set ctl = Nothing
End Sub
With the rewrite, I'll throw Me in after the call on resize and initially, the objects are where they should be, centered accordingly. Once I try to resize the form, no matter what, the objects move in a southeast direction away from center. In the rewrite, it seems the values passed for top and left are the current locations of each object. Is this assumption correct? How would I get the values of the original placement of objects? Do I need to build a table every time this applyed to a form to get those distances?