Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Textbox background color if empty

Status
Not open for further replies.

lizray

Programmer
May 14, 2008
126
AU
I have a textbox and want to change the background color to green when ever it contains text.ie after a keypress. So that when a user types a char, it will immediately change to green and stay green until the user deletes all chars. I want thisctolor change to occur before the update event. I have tried the key events and change event for the control, but they seem to occur before the char is actually stored in the control. (conditional formatting doesnt work as it works when upon leaving the control, whereas I want the change to occur while still in the control)
I have tried the following for all key events:
if isnull(txtbox) then txtbox.backcolor=vbwhite else txtbox.backcolor=green
if isempty(txtbox) then txtbox.backcolor=vbwhite else txtbox.backcolor=green
 
Try
Code:
Private Sub txtBox_KeyUp(KeyCode As Integer, Shift As Integer)
    If Len(Me.txtBox.Text) > 0 Then
        Me.txtBox.BackColor = vbGreen
     Else
        Me.txtBox.BackColor = vbWhite
    End If
End Sub

Duane
Hook'D on Access
MS Access MVP
 
You problem is you have a fundamental misunderstanding of when the "value" of a control updates. When you modify text in a control the information is cached until you update it. The update is usually triggered on leaving the control. So as Duane points out the trick is you have to use the "text" property. You can use the change event. So that you can see this:

Code:
Private Sub test_Change()
 MsgBox test.Text
 MsgBox Nz(test.Value, "No Value")
End Sub

You will see that you can have all kinds of text in the textbox but the value is null until leaving.
 
Thanks Duane, it worked perfectly. Thanks MajP for the extra understanding of the situation.
I still have a small problem with comboboxes, all of the drop down selections uses the same active color (ie green) and I would like the selection highlighted in the drop down list to be white as it now shows as purple.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top