Function CenterForm(strFormName As String, _
Optional varFarLeft As Variant)
Dim frm As Form
Dim ctl As Control
Dim ctlChild As Control
Dim tabElement As Control
Dim i As Integer
Dim lngLeftOffset As Long
Dim lngWidth As Long
Dim lngWidthChild As Long
Dim x As Long
Set frm = Forms(strFormName)
Application.Echo False
lngLeftOffset = (frm.InsideWidth \ 2) - (frm.Width \ 2)
If (Not IsMissing(varFarLeft)) Then
If (varFarLeft + lngLeftOffset < 0) Then Exit Function
Else
x = frm.Width
For Each ctl In frm.Controls
If (ctl.Left < x) Then x = ctl.Left
Next
If (x + lngLeftOffset < 0) Then Exit Function
End If
For Each ctl In frm.Controls
If (ctl.ControlType = acTabCtl) Then
GoSub MoveTabCtl
ElseIf (ctl.Parent.Name = strFormName) And (ctl.ControlType = acOptionGroup) Then
Set tabElement = ctl
GoSub MoveOptionGroup
ElseIf (ctl.Parent.Name = strFormName) And (ctl.ControlType <> acOptionGroup) And (ctl.ControlType <> acTabCtl) Then
On Error Resume Next
If (ctl.ControlType <> 103) Then
For Each ctlChild In ctl.Controls
ctlChild.Left = ctlChild.Left + lngLeftOffset
Next
End If
30:
Err.Clear
On Error GoTo ErrHandler
ctl.Left = ctl.Left + lngLeftOffset
End If
Next
'********************
'* Exit Procedure *
'********************
ExitProcedure:
Application.Echo True
Exit Function
'****************************
'* Error Recovery Section *
'****************************
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitProcedure
MoveTabCtl:
lngWidth = ctl.Width
For i = 0 To ctl.Pages.Count - 1
For Each tabElement In ctl.Pages(i).Controls
If (tabElement.ControlType = acOptionGroup) Then
GoSub MoveOptionGroup
Else
If (tabElement.Parent.Name <> strFormName) Then
If (tabElement.Parent.ControlType <> acOptionGroup) Then
If (tabElement.Parent.Parent.ControlType <> acOptionGroup) Then tabElement.Left = tabElement.Left + lngLeftOffset
End If
End If
End If
Next
Next i
ctl.Left = ctl.Left + lngLeftOffset
ctl.Width = lngWidth
Return
MoveOptionGroup:
lngWidthChild = tabElement.Width
For Each ctlChild In tabElement.Controls
ctlChild.Left = ctlChild.Left + lngLeftOffset
Next
tabElement.Left = tabElement.Left + lngLeftOffset
tabElement.Width = lngWidthChild
Return
End Function