Order when cycling through form controls (1 Viewer)

Alc

Registered User.
Local time
Yesterday, 21:04
Joined
Mar 23, 2007
Messages
2,407
This may seem like an odd question, but it's puzzling me.

I have the following code behind a button on a form
Code:
    Dim ctl as Control

    For Each ctl In Me.Controls
        MsgBox ctl.name
    Next ctl

What I'm curious about is the order in which the fields are cycled through. Is it always the same? In my test form, it appears to be based on the Tab order, but is this always going to be true?
 

Alc

Registered User.
Local time
Yesterday, 21:04
Joined
Mar 23, 2007
Messages
2,407
Thanks a lot. I was hoping it was tab order but I guess since the tab order matches the order I added the fields, in most cases, that was just the impression I was getting.

I think I have a way around what I was trying to do.
 

isladogs

MVP / VIP
Local time
Today, 01:04
Joined
Jan 14, 2017
Messages
18,186
I just did a quick test on a form before and after rearranging the tab order.
I also changed your code to Debug.Print for convenience

The control names were listed in exactly the same order each time which supports the previous comments
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 20:04
Joined
Feb 28, 2001
Messages
27,001
If you don't rename the controls on the form, or if they are unbound, you will see a number that is appended to each control. So "Listbox31" and "Textbox42" and "CommandButton13" and so forth. This number is reflective of the order in which the control object was created and probably is reflective of some autonumbered key. I THINK (subject to correction) that in every case for a form and/or report, there is an Items collection and the item index reflects the order of programming appearance. If it is treated as a structural PK then you cannot re-order the controls for the "For Each" construct. You can see this if you use the Locals window during debugging of any code defined in a class module, such as an Event routine.

However, as noted, you can order the controls for display purposes using the .TabOrder property.
 

Users who are viewing this thread

Top Bottom