IF statments to change the colors of BackColor on my form

M

Mike

My code below works at changing a text box (txtCurrentStatus)
background color (BackColor) to Red when "Open" is selected from a
Combo Box (ComboCurrentStatus) and Blue when it is not.

What I want is:
IF the ComboCurrentStatus is selected to "Open" I want the the text box
(txtCurrentStatus) to be RED.

IF the ComboCurrentStatus is selected to "Action Track" I want the the
text box (txtCurrentStatus) to be YELLOW.

IF the ComboCurrentStatus is selected to "Closed" I want the the text
box (txtCurrentStatus) to be GREEN.

Can anyone help me with my coding?
---------------------------------------------------------------------
Private Sub ComboCurrentStatus_AfterUpdate()
If Me!txtCurrentStatus = "Open" Then
Me!txtCurrentStatus.BackColor = vbRed
Else
Me!txtCurrentStatus.BackColor = vbBlue

End If
End Sub
 
J

John Vinson

What I want is:
IF the ComboCurrentStatus is selected to "Open" I want the the text box
(txtCurrentStatus) to be RED.

IF the ComboCurrentStatus is selected to "Action Track" I want the the
text box (txtCurrentStatus) to be YELLOW.

IF the ComboCurrentStatus is selected to "Closed" I want the the text
box (txtCurrentStatus) to be GREEN.

Since you have three branches, I'd suggest using a SELECT CASE
statement rather than an If (which is limited to two, unless you nest
If blocks). I presume the name of the control is ComboCurrentStatus, I
have no idea where you got txtCurrentStatus, which would refer to a
different control. I'm guessing you're selecting a status from
ComboCurrentStatus and changing the color of a separate textbox,
txtCurrentStatus:

Private Sub ComboCurrentStatus_AfterUpdate()
Select Case Me!ComboCurrentStatus
Case "Open"
Me!txtCurrentStatus.BackColor = vbRed
Case "Action Track"
Me!txtCurrentStatus.BackColor = vbYellow
Case "Closed"
Me!txtCurrentStatus.BackColor = vbGreen
Case Else
MsgBox "Oops, don't know the status!"
End Select
End Sub

You may also want to put the same code in the Form's Current event to
properly color the textbox for existing records; the AfterUpdate will
apply only when you change the value in the combo box.


John W. Vinson[MVP]
 
M

Mike

Perfect! Thanks!
John said:
Since you have three branches, I'd suggest using a SELECT CASE
statement rather than an If (which is limited to two, unless you nest
If blocks). I presume the name of the control is ComboCurrentStatus, I
have no idea where you got txtCurrentStatus, which would refer to a
different control. I'm guessing you're selecting a status from
ComboCurrentStatus and changing the color of a separate textbox,
txtCurrentStatus:

Private Sub ComboCurrentStatus_AfterUpdate()
Select Case Me!ComboCurrentStatus
Case "Open"
Me!txtCurrentStatus.BackColor = vbRed
Case "Action Track"
Me!txtCurrentStatus.BackColor = vbYellow
Case "Closed"
Me!txtCurrentStatus.BackColor = vbGreen
Case Else
MsgBox "Oops, don't know the status!"
End Select
End Sub

You may also want to put the same code in the Form's Current event to
properly color the textbox for existing records; the AfterUpdate will
apply only when you change the value in the combo box.


John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top