Changing properties with a loop

  • Thread starter Thread starter Justin E.
  • Start date Start date
J

Justin E.

is there a way to change the properties of a large number
of objects on a form. IE i have a contact list that needs
to have all of the text boxes locked and unlocked using a
command button. I can do it object by object with
objname.locked = true. but its cumbersome and i am
finding i have to do this on several other long forms.
can anyone help?
 
There are a couple of ways to do this.

1) You could give them sequential names such as txtTextbox1, txtTextbox2,
txtTextbox3, etc. You could then use a For..Next Loop to address them.

For i = 1 to 10
Me.Controls("txtTextbox" & i).Enabled = False
Next i

2) Another method is to loop through all the controls on the form.

Dim ctl As Control
For Each ctl in Me.Controls
If ctl.ControlType = acTextbox Then
ctl.Enabled = False
End If
Next
 
Justin said:
is there a way to change the properties of a large number
of objects on a form. IE i have a contact list that needs
to have all of the text boxes locked and unlocked using a
command button. I can do it object by object with
objname.locked = true. but its cumbersome and i am
finding i have to do this on several other long forms.
can anyone help?

To lock or unlock ALL text controls when a command button is clicked,
code the Click event of a command button:

Dim C as Control
For each C in Me.Section(0).Controls
If TypeOf C is TextBox Then
C.locked = Not C.Locked
End If
Next

This will turn on or off the locked property of each Text Control in the
Detail section of a form whenever the button is clicked.

Include ComboBoxes, ListBoxes in the TypeOf .. Is line if needed.
 
fredg said:
To lock or unlock ALL text controls when a command button is clicked,
code the Click event of a command button:

Dim C as Control
For each C in Me.Section(0).Controls
If TypeOf C is TextBox Then
C.locked = Not C.Locked
End If
Next

This will turn on or off the locked property of each Text Control in the
Detail section of a form whenever the button is clicked.

Include ComboBoxes, ListBoxes in the TypeOf .. Is line if needed.

Correction>>>>>
I should have said:
Include ComboBoxes, ListBoxes in the TypeOf .. Is line (using an OR
expression) if needed.
If TypeOf C is TextBox OR TypeOf C is ComboBox, Or ...etc... Then
 
Back
Top