Link to home
Start Free TrialLog in
Avatar of Henry_Harris
Henry_HarrisFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Order of Controls in Controls Collection

Is it possible to predict the order of controls when looping through the controls collection.
Order on the form would be preferred or else Tab Order will suffice.
My code is attached
Public Function CheckForEmptyControls()
CheckForEmptyControls = True
With CodeContextObject
    Dim ctl As Control
    Dim strMissingEntryMessage As String
    strMissingEntryMessage = ""
    For Each ctl In .Controls
        If ctl.Visible And _
            (Left(ctl.Name, 3) = "txt" Or Left(ctl.Name, 3) = "cbo") _
            And ctl.Name <> "txtRelease" And ctl.Name <> "ID" And IsNull(ctl) Then
                strMissingEntryMessage = strMissingEntryMessage & Chr(13) & Chr(10) & " " & ctl.Tag
        End If
    Next ctl
    If strMissingEntryMessage <> "" Then
        CheckForEmptyControls = False
        If MsgBox("The following needs to be completed:" & Chr(13) & Chr(10) & strMissingEntryMessage, _
            vbRetryCancel, strMsgBoxTitle & ": Completing Form " & .Name) = vbCancel Then
                .Undo
                .cmdCloseForm.SetFocus
                .cmdSaveRecord.Enabled = False
        End If
    End If
End With
End Function

Open in new window

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

If I'm not mistaken, the order in which the controls are added to the Controls collection is dependent on how they were added to the form and not the Tab order. In other words, if I add Control1, Control2 and Control3, my Controls Collection would be in that order. If I then deleted Control1 and add it back, my collection order would be

Control2
Control3
Control1

What's your end goal here? Why would it matter what physical order the Controls collection would be in? There may be other ways to accomplish what you're after, but we'd need to know more about it.
Avatar of Henry_Harris

ASKER

Thanks for your post, which I understand
As you can see from my code snippet, I will be displaying amessage that lists the Tags of the fields that have not been completed. I would prefer the order that the Msgbox message is constructed to be in the same order as the controls on the form. Constructing the message in Tab order would allow me change controls and maintain a predictable order of controls in the message.
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

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
This solution is nearly perforect for what I need.
Thanks very much.