Changing Text Colors on Form

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

Guest

I have a form that about 30 people use to enter data on a daily basis. I
wanted to offer color options via simple buttons, like a bright, a soft etc.
I have this so far:

Me.Section(0).BackColor = vbBlue
Me.Text1.ForeColor = vbYellow

Is there a way to specify all text boxes or all labels instead of listing
all of them?
 
Yes, sort of. You can't specify all textboxes, but you can loop through the
controls on the form and if the type of control is a textbox, make the
change.

Example:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
ctl.ForeColor = vbYellow
End If
Next

You can do other things as well to do this if you want to only modify some
textboxes.

1) You can use the Tag property of the control. Place a value in the Tag
property and check that value using an If statement. Make the changes as
appropriate. This will allow you to "group" controls by their Tag value.

2) Name the controls using consecutive numbers that can be looped through.
For example, txtMyTextbox1, txtMyTextbox2. You can then use an alternate
syntax for referring to a control to loop through the controls.

Example:
For i = 1 To 10
Me.Controls("txtMyTextbox" & i).ForeColor = vbYellow
Next

Using this method, you can group the controls by their name, using the
number to refer to individual controls within the group.
 
You can enumerate through the controls on the form and do different things
to different types of controls:
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox
ctl.ForeColor = vbYellow
Case acLabel
ctl.ForeColor = vbBlue
Case Else
ctl.ForeColor = vbBlack
End Select
Next ctl

If you want to reset everything back to black:
Dim ctl As Control
For Each ctl In Me.Controls
ctl.ForeColor = vbBlack
Next ctl
 
Thank you so much Wayne and Mark! I wasn't sure that they were called
"controls". This will work great. I am trying to accomodate users who may
be color blind or just have different preferences but not as many options as
a color picker like I found on Leban's site.
 
Back
Top