VBA Looping through objects on Form

shamas21

Registered User.
Local time
Today, 18:08
Joined
May 27, 2008
Messages
162
Hi All

I have a form called Form1.

I have several different objects on it i.e. buttons, textboxes, comoboxes etc.

All i want is a piece of code that runs through all the objects on Form1 and gives me their names.

Thanks
 
something like

dim ctrl as control

for each ctrl in form.controls
msgbox(ctrl.name)
next
 
Hi

This doesnt work.

it brings back a message saying

'Object or class does not support the set of events'
 
Gemma's code is correct. There must be some other problem. When the error occurs does it break you into the debugger at a particular line, or does code fail to run altogether? If the latter, in a code window, go to Menu->Debug->Compile and does this highlight a particular line of code?
 
Gemma's code is correct. There must be some other problem. When the error occurs does it break you into the debugger at a particular line, or does code fail to run altogether? If the latter, in a code window, go to Menu->Debug->Compile and does this highlight a particular line of code?


it says variable not defined. It then points to the section where it says 'Form'
 
So you've solved 'Object or class does not support the set of events?'

You need to replace the word 'form' with a reference to your form.
 
So you've solved 'Object or class does not support the set of events?'

You need to replace the word 'form' with a reference to your form.


Hi

It still doesnt work

I use the following code
Code:
Dim ctrl As Control
For Each ctrl In frmAPI_DTP.Controls
MsgBox (ctrl.Name)
Next

Ive even tried using the following code without any luck

Code:
Dim ctrl As Control
For Each ctrl In CurrentProject.AllForms.Item(0).Control
MsgBox (ctrl.Name)
Next

I think the form doesnt have a control property
 
If the code is behind the form in question ...

For Each ctrl In Me.Controls

If the code is in a standard module AND the form is OPENED ...

For Each ctrl In Forms("frmAPI_DTP").Controls


>> I think the form doesnt have a control property <<

You are right ... but it DOES have the Controls collection, as indicated in the samples. :)

... Gotta run! ...
 
If the code is behind the form in question ...

For Each ctrl In Me.Controls

If the code is in a standard module AND the form is OPENED ...

For Each ctrl In Forms("frmAPI_DTP").Controls


>> I think the form doesnt have a control property <<

You are right ... but it DOES have the Controls collection, as indicated in the samples. :)

... Gotta run! ...
I think this is correct. Anytime I have done this, it has been written any of these 3 ways:
Code:
for each ctrl in [B]me.controls[/B]
Code:
for each ctrl in forms("FORMname").controls
Code:
for each ctrl in forms!FORMname!.controls
 
Hello Adam (and the rest of the thread!) ...

>> for each ctrl in forms!FORMname!.controls <<

... ummm ... you would not reference the Controls collection with a bang (!) and a dot (.) ... :eek: .... (typo I am sure!)

...

Another way to reference the collection is IF the form has a module behind it, then you can reference the form through its class object ... if the form is not open, refering to the class will open it during the time in which the class is instantiated ...

For Each ctrl in <form_class_name>.Controls

Where <form_class_name> is the name of the form class from the VBA editor. Samples of the class name would be as follows ...

With a form named "Form1" the form's class would be: "Form_Form1"
 
Hello Adam (and the rest of the thread!) ...

>> for each ctrl in forms!FORMname!.controls <<

... ummm ... you would not reference the Controls collection with a bang (!) and a dot (.) ... :eek: .... (typo I am sure!)
No, it's not a typo...actually, I've done this quite frequently, and the intellisense dropdown always shows up after the BANG symbol if you put a DOT after it. :D I am not a programmer, so I would have not known the difference. But anyway, I'm taking off now. Thank you for the correction. I hope the OP gets what he needs out of it...
 
Fantastic Guys, it works perfect now!

Thanks for the help!
 

Users who are viewing this thread

Back
Top Bottom