need help changing colors of text box

  • Thread starter Thread starter photogman
  • Start date Start date
P

photogman

I'm a newbie that is trying to change the color of the background of a
textbox when a checkbox is checked (-1) next to it. So that if the check
box is not checked the textbox next to it will stay the same color, but if
the check box is checked, the textbox will turn let's say gray.

I was able to do it with the whole form background itself, but don't know
how to do it with one textbox. The code that I acquired for the whole form
itself is:

Private Sub Form_Current()
If BOX.Value = -1 Then
Section(0).BackColor = RGB(192, 192, 192)
Section(1).BackColor = RGB(192, 192, 192)
Else
Section(1).BackColor = RGB(12, 255, 255)
Section(0).BackColor = RGB(12, 255, 255)
End If
End Sub




I believe all I need is to understand what the line of code would be for a
particular text box.

Thank you to all that can help me. This may seem very basic to a lot of
you, but for a newbie like myself, can seem like an insurmountable mountain.
 
Try this...

Private Sub CheckBox_AfterUpdate()
With Me
If .CheckBox Then
.Combo.BackColor = vbGreen
Else
.Combo.BackColor = vbWhite
End If
End With
End Sub

Paste this code into the AfterUpdate event of your checkbox (rename
contrrols appropriately). Substitute the colours for the vb colour
constants you would like.

Jamie :o)
 
The clue is in the property being changed -- the .BackColor property.

Try setting

YourTextBox.BackColor = ...

Good luck

Jeff Boyce
<Access MVP>
 
Thanks Jeff, I got it to work with the following code:

Private Sub conbox1_AfterUpdate()
If conbox1.Value = -1 Then
con1.BackColor = RGB(255, 255, 255)
Else
con1.BackColor = RGB(247, 148, 29)
End If
End Sub

The only problem is everytime I leave the form and come back in, all the
color changes have reverted back to their original color and I have to go
and re-check the checkmark textbox and uncheck them to get it to change
colors again. Any hints here that can make the changes permanent? Thanks
 
One more thing Jeff, the changes on one record are applying to all the
records, which isn't what I intended.
 
One more thing Jeff, the changes on one record are applying to all the
records, which isn't what I intended.

This is normal behavior when the Check Box is not bound to a field in
the table.

What version of Access are you using. If Access 2000 or newer you can
use the text control's Conditional Formatting property.

1) Add a Yes/No check box field to the underlying table.
2) Add this new field to the form (from the Field List).
3) Select the text control whose color you wish to change.
Click on Format + Conditional Formatting
Set the Condition1 drop down box to
Expression Is
In the box along side the drop-down write
[CheckBoxName] = -1
Set the backcolor as desired.
Save the changes.
 
Fred, it worked just like you indicated it would!! Thank you very much and
much thanks to the others that also responded to my request for help!


fredg said:
One more thing Jeff, the changes on one record are applying to all the
records, which isn't what I intended.

This is normal behavior when the Check Box is not bound to a field in
the table.

What version of Access are you using. If Access 2000 or newer you can
use the text control's Conditional Formatting property.

1) Add a Yes/No check box field to the underlying table.
2) Add this new field to the form (from the Field List).
3) Select the text control whose color you wish to change.
Click on Format + Conditional Formatting
Set the Condition1 drop down box to
Expression Is
In the box along side the drop-down write
[CheckBoxName] = -1
Set the backcolor as desired.
Save the changes.
 
Back
Top