Writing Code to Reference All Objects on a Form/Section

akt01

Registered User.
Local time
Today, 00:55
Joined
Jun 19, 2003
Messages
21
I am writing code for an Access form. The form has a button and numerous objects. When the button is clicked, I want one of the properties in all of the objects to change (for example, the BackColor in all objects should turn grey).

Instead of writing code to individually change the BackColor for each of the numerous objects, is there a way to write a single code to simultaneously reference all of the objects on a form?

That way, as I add objects, I would not have to change that code.

Does any one know a way to do this?


Thanks
 
Cycle though the control list on your form:

dim ctrl as control
dim frmActiveForm as form

set frmActiveForm = Screen.Activeform
for each ctrl in frmActiveform
if ctrl.controltype = acTextBox then _
ctrl.backcolor = acRed
next

The foregoing is very simplistic, but should get you started.

Note that you have to check controltype, because you can't change the color of all controls, eg a button, etc.
 
Good ideas, llkhoutx.

You can simplify the code slightly like this:
Sub ChangeProperties()
Dim ctl as Control
  For each ctl In Me.Controls
    'put your code here
  Next ctl
End Sub

Search the Access online help for info on control types.
 
The advantage of llkhoutx's code is that you can put it in a stand alone module and call it from any form but dcx's is a little more efficient. Amalagamate the two and you have the ideal solution! llkhoutx's statement about contol types still stands though.

Sub ChangeProperties()
Dim ctl as Control
For each ctl In screen.activeform
'put your code here
Next ctl
End Sub

ps guys I've copyrighted this one:p
 

Users who are viewing this thread

Back
Top Bottom