How to iterate through controls on a form (1 Viewer)

catbeasy

Registered User.
Local time
Today, 10:59
Joined
Feb 11, 2009
Messages
140
I know you can iterate through fields in tables, can you do the same in Access forms?

I'm looking to do something like:

For Each control In frmName
do code..
Next

But not sure of the exact syntax..or if even possible to do..

Thanks for any assistance..
 

ajetrumpet

Banned
Local time
Today, 12:59
Joined
Jun 22, 2007
Messages
5,638
CONTROLS is a collection of a form. a sample loop would be:
Code:
dim c as control

for each c in forms("formname").controls

  debug.print c.name & ", " & c.type

next c
 

boblarson

Smeghead
Local time
Today, 10:59
Joined
Jan 12, 2001
Messages
32,059
Code:
Dim ctl As Control

For Each ctl in Me.Controls
   Whatever...
Next

And if you refer to a specific form where the code is not running then it would be

For Each ctl in Forms!YourFormName.Controls
 

ChrisO

Registered User.
Local time
Tomorrow, 03:59
Joined
Apr 30, 2003
Messages
3,202
It still amazes me just how flexible VBA is. (Flexibility by design.)

Code:
Private Sub cmdTest_Click()
    
    For Each Control In Me
        MsgBox Control.Name
    Next

End Sub

The above code works under specific conditions but I would not use it as is.

Regards,
Chris.
 

missinglinq

AWF VIP
Local time
Today, 13:59
Joined
Jun 20, 2003
Messages
6,423
The code that's been posted so far is safe, because all you're doing is listing/printing out the names of all controls, and all controls have names. But when you actually start to loop thru controls to do something practical, such as disabling controls, for example, you have to start being a little more careful! Code like this:

Code:
Dim ctl As Control
For Each ctl In Me.Controls
    ctl.Enabled = False   ' Disable the control
Next ctl
will work without problem, assuming all controls on the form have the Enabled Property! But Labels don't have the Enabled Property, so if you use this code and have a Label on your form, the code will error out!

There's a couple of ways to get around this. One is to tell Access to simply ignore any errors encountered when looping the controls:

Code:
Dim ctl As Control
For Each ctl In Me.Controls
   [B]On Error Resume Next[/B]
   ctl.Enabled = False   '  to Disable
   [B]Err.Clear[/B]
Next ctl
There's some difference of opinion as to whether this is good technique or not. But there's another method that can be used in this situation and in situations where you only want to loop thru certain kinds of controls.

Code:
For Each ctl In Me.Controls
     If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then 
         ctl.Enabled = False
     End If
 Next ctl
This will only loop thru Textboxes and Comboboxes, and leave other controls alone.

At other times you may want to be even more select in which controls you loop thru. You may only want to loop thru some textboxes, but not all textboxes. To accomplish this, you'll need to use the Tag Property of the controls. Start by setting the Tag Property of the controls you want to loop thru to some arbitrary word. In this sample we'll use ControlToLoop.

In Design View, select all of the controls you want to loop thru, then goto Properties – Other and in the Tag Property box type in ControlToLoop, without quotation marks. Then use this code, in the appropriate event:

Code:
Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.Tag = "ControlToLoop" Then
       ctl.Enabled = False
    End If
Next ctl

Now only the controls with the tag ControlToLoop will be disabled. To selectively loop thru a different groups of controls, you can assign different tags to different groups and code accordingly.
 

Users who are viewing this thread

Top Bottom