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 = TrueWith 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 IfEnd WithEnd Function
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.
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.