How - colour empty text box

  • Thread starter Thread starter Boxer
  • Start date Start date
B

Boxer

How can I put for exam. red colour if text box is empty, but if I put some
data into the colour change into white.
Please is some kind of hurry:((((((
 
hi,
if isnull(me.textbox1) then
me.textbox1.backcolor = 255 'red
end if
put in form's on open event.
or textbox's before update event
 
I would write a generic sub that would check the textbox for null and
highlight red if it was null or white (normal) if it wasn't

Private Sub HighlightTextBox(txtBox As TextBox)

If IsNull(txtBox.Value) Then
'if the field is null - back color red
txtBox.BackColor = vbRed
Else
'otherwise white
txtBox.BackColor = vbWhite
End If

End Sub

I would then have some code in the open/load event for the form that
would loop through the text boxes you wanted to check for null like so;

Private Sub Form_Load()

Dim ctl As Control
Dim ctls As Controls

Set ctls = Me.Form.Controls

For Each ctl In ctls
'Loop through each control at startup to set the initial
highlighting
If ctl.ControlType = 109 Then
'Only validate textboxes this way
'you might want to specify which textboxes
'or have different validation for other controls
HighlightTextBox ctl
End If
Next ctl

End Sub

Finally I would have call the sub again from each relevent text boxes
lost focus event - this would remove the highlighting if the textbox
had a value. E.g.

Private Sub txtBox1_LostFocus()

'the lost focus event of each control should also call the
validation procedure
HighlightTextBox txtBox1

End Sub
 
Boxer said:
How can I put for exam. red colour if text box is empty, but if I put some
data into the colour change into white.


You can use Conditional Formatting to do this.

Expression Is: IsNull([text0])
 
Back
Top