We help IT Professionals succeed at work.
Get Started

Why does this function only work after I put in a breakpoint?

leachj asked
Last Modified: 2012-05-12
I am trying to center a tab control on a form.  I maximize the form on load event.  When I call this function, the tab control does not center and the application hangs when I try to go back into design mode.  I have to shut down access and restart it.  If I place a breakpoint  at the line where I set the frm variable, then continue execution after the break,  the tab control centers fine and I can use the application ok.  I can place the breakpoint on the next line and get the same result.  Placing the breakpoint anywhere after that and the application will not center the tab control and I get the hang when going back to design mode.


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.WindowWidth \ 2) - (frm.Width \ 2)
    If (Not IsMissing(varFarLeft)) Then
        If (varFarLeft + lngLeftOffset < 0) Then Exit Function
        x = frm.Width
        For Each ctl In frm.Controls
            If (ctl.Left < x) Then x = ctl.Left
        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
            End If
            On Error GoTo ErrHandler
            ctl.Left = ctl.Left + lngLeftOffset
        End If

'*  Exit Procedure  *

    Application.Echo True
    Exit Function

'*  Error Recovery Section  *
    MsgBox Err.Description, vbExclamation
    Resume ExitProcedure


    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
                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 i
    ctl.Left = ctl.Left + lngLeftOffset
    ctl.Width = lngWidth


    lngWidthChild = tabElement.Width
    For Each ctlChild In tabElement.Controls
        ctlChild.Left = ctlChild.Left + lngLeftOffset
    tabElement.Left = tabElement.Left + lngLeftOffset
    tabElement.Width = lngWidthChild


End Function

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE