Option Compare Database
Option Explicit
' FittedTab Class
' This class fits the pages of a tab control either Height or Height and width
' 2011
' Developer Maj P
'
' Methods:
' InitFitted - Used as a pseudo constructor to initialize the class by passing the tab control.
Private WithEvents mTabCtl As Access.tabControl
Private WithEvents mFrm As Access.Form
Private mControlsOnTab As Collection
Private mPageTop As Long
Private mPgHeight As Long
Private mPgWidth As Long
Private mFitWidth As Boolean
Public Sub InitFittedTab(TheTabControl As Access.tabControl, Optional FitWidth As Boolean = False)
Set mTabCtl = TheTabControl
Set mFrm = TheTabControl.Parent
mPageTop = TheTabControl.Pages(0).Top
mTabCtl.OnChange = "[Event Procedure]"
mFrm.OnCurrent = "[Event Procedure]"
mFitWidth = FitWidth
LoadControlsOnTab
SaveControlPositions
'SetPgHeight
'SetPgWidth
'ShrinkAll
'ResizeActivePageControls
'FitPage
End Sub
Private Sub SaveControlPositions()
Dim ctl As Access.Control
For Each ctl In mControlsOnTab
ctl.Tag = ctl.Left & ";" & ctl.Width & ";" & ctl.Top & ";" & ctl.Height
'Debug.Print ctl.Name & " " & ctl.Tag
Next ctl
End Sub
Public Sub ShrinkAll()
Dim ctl As Access.Control
For Each ctl In mControlsOnTab
With ctl
.Height = 0
.Top = mPageTop
.Width = 0
End With
Next ctl
End Sub
Private Function CtlOnActivePage(ByVal ctl As Access.Control) As Boolean
CtlOnActivePage = (ctl.Parent Is mTabCtl.Pages(mTabCtl.Value))
End Function
Private Sub mTabCtl_Change()
SetPgHeight
SetPgWidth
ShrinkAll
ResizeActivePageControls
FitPage
End Sub
Public Sub SetPgHeight()
Dim ctl As Access.Control
Dim tempTop As Long
For Each ctl In mControlsOnTab
If CtlOnActivePage(ctl) Then
If ctl.Top > tempTop Then
mPgHeight = (ctl.Top - mPageTop) + ctl.Height
End If
tempTop = ctl.Top
End If
Next ctl
'debug.print mPgHeight
End Sub
Public Sub SetPgWidth()
Dim ctl As Access.Control
Dim tempWidth As Long
For Each ctl In mControlsOnTab
If CtlOnActivePage(ctl) Then
If ctl.Left + ctl.Width > tempWidth Then
mPgWidth = ((ctl.Left - mTabCtl.Left) + ctl.Width)
End If
tempWidth = ctl.LeftPadding + ctl.Width
End If
Next ctl
End Sub
Private Sub LoadControlsOnTab()
Dim ctl As Access.Control
Dim pg As Access.Page
Set mControlsOnTab = New Collection
For Each pg In mTabCtl.Pages
For Each ctl In pg.Controls
mControlsOnTab.Add ctl
Next ctl
Next pg
End Sub
Private Sub ResizeActivePageControls()
Dim ctl As Access.Control
For Each ctl In mTabCtl.Pages(mTabCtl.Value).Controls
With ctl
.Left = Val(Split(ctl.Tag, ";")(0))
.Width = Val(Split(ctl.Tag, ";")(1))
.Top = Val(Split(ctl.Tag, ";")(2))
.Height = Val(Split(ctl.Tag, ";")(3))
End With
Next ctl
End Sub
Private Sub FitPage()
mTabCtl.Height = mPgHeight
If mFitWidth Then
mTabCtl.Width = mPgWidth
End If
End Sub