Link to home
Start Free TrialLog in
Avatar of leachj
leachj

asked on

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

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.

Thanks,
John

 
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
    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

Open in new window

Avatar of mbizup
mbizup
Flag of Kazakhstan image

Does the code work if the Maximize code is not there, or if it is run from a later event such as the Current event (or even a button click)?
What happens if you comment out   Application.Echo False  ?

mx
ASKER CERTIFIED SOLUTION
Avatar of leachj
leachj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leachj
leachj

ASKER

Figured it out by futzing with it.
Yep ... my next comment ... DoEvents
Shouldn't lngLeftOffset be dblLeftOffset?  - you are dividing a width by 2 which could be an odd number, and you will wind up with an amount containing a decimal amount.  Long will round that off.
BTW what was the futz solution?
thanks all.