Avatar of Henry_Harris
Henry_Harris
Flag 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

Microsoft Access

Avatar of undefined
Last Comment
Henry_Harris

8/22/2022 - Mon
Scott McDaniel (EE MVE )

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.
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
Scott McDaniel (EE MVE )

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Henry_Harris

ASKER
This solution is nearly perforect for what I need.
Thanks very much.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck