We help IT Professionals succeed at work.

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

leachj
leachj asked
on
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
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)?
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
What happens if you comment out   Application.Echo False  ?

mx
Commented:
mbiz, No it does not .
DatabaseMX, Same result.

That being said, I solved the issue by inserting a DoEvents after the line
Application.Echo False
I'm guessing the module needed a fraction of time to complete calculations.

Thanks for the responses.

Author

Commented:
Figured it out by futzing with it.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
Yep ... my next comment ... DoEvents

Commented:
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.

Commented:
BTW what was the futz solution?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
DoEvents (http:#a37079207)

Commented:
thanks all.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.