Obtaining a List of Control Names on a Form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings,
I am trying to obtain the names of each control in a form. I was hoping
to use code similar to the following:

For each x in forms("DataEntry").controls
debug.print x.name
next x

However, I receive an "Object doesn't support this property or method" error
message when I attempt to execute this routine. Apparently there is not a
"name" property for the controls collection on a form. How can I obtain a
list of all controls on a form? Any suggestions?

Thanks in advance.
 
Works for me ...

Public Sub TestControlName()

Dim x As Control
For Each x In Forms(0).Controls
Debug.Print x.Name
Next x

End Sub

.... runs without error for me.

Of course, the form in question must be open when the code runs.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Greetings,
I am trying to obtain the names of each control in a form. I was hoping
to use code similar to the following:

For each x in forms("DataEntry").controls
debug.print x.name
next x

However, I receive an "Object doesn't support this property or method" error
message when I attempt to execute this routine. Apparently there is not a
"name" property for the controls collection on a form. How can I obtain a
list of all controls on a form? Any suggestions?

Thanks in advance.

Where are you running this code from?
If on the form "DateEntry":

Dim c As Control
For Each c In Me.Controls
Debug.Print c.Name
Next c

If in a Module, the form must be open(ed) before you run the code:

Public Sub controlNames()
DoCmd.OpenForm "DataEntry", acNormal, , , , acHidden
Dim c As Control
For Each c In Forms("DataEntry").Controls
Debug.Print c.Name
Next c
End Sub
 
Back
Top