We help IT Professionals succeed at work.

text box change background color based on input  (vba)

Bobo--just_East_of_Madison
Bobo--just_East_of_Madison asked
on
Is it possible to change the background color of a text box based on the input? See my coding example. I tried it but it did not work.

Thanks.


ttpageremoval.Value = True Then
ttrpageremovaltextbox1.BackColor = RGB(255, 0, 0)
ElseIf ttrpageremovaltextbox1.Value = "404" Then
ttrpageremovaltextbox1.BackColor = RGB(255, 255, 255)

End If
End Sub

Open in new window

Comment
Watch Question

Commented:
What event are you attempting to set this in?  Also, you first test for a boolean value and then test for a string value.  This might be causing your problem.  The following works for me.
OM Gang


Private Sub Text0_AfterUpdate()

On Error GoTo Err_Text0_AfterUpdate

    If Me.Text0 = "True" Then
        Me.Text0.BackColor = RGB(255, 0, 0)
    ElseIf Me.Text0 = "404" Then
        Me.Text0.BackColor = RGB(255, 255, 255)
    Else
        Me.Text0.BackColor = RGB(255, 255, 0)
    End If
   

Exit_Text0_AfterUpdate:
    Exit Sub

Err_Text0_AfterUpdate:
    MsgBox Err.Number & " (" & Err.Description & ") in procedure Text0_AfterUpdate of VBA Document Form_Form19"
    Resume Exit_Text0_AfterUpdate

End Sub