Change backcolor with IsNull

A

amy14775

I am trying to set my program up so on a form if a field the user needs to
fill in is null then it will appear yellow and if it is filled like it should
be then red backcolor. I don't want to set them up as required fields, just
reminding the user these fields are suggested before printing. I have been
trying to use the below code and it just sits there no error codes or
anything. I have placed the code on the onOpen event for the form.Any help
would be greatly appreciated. I don't have experience with IsNull.Thank you

Dim lngYellow As Long
Dim lngRed As Long

lngYellow = RGB(255, 255, 0)
lngRed = RGB(255, 0, 0)

If IsNull(Me.txtBillReceived) Then
Me.txtBillReceived.BackColor = lngYellow
Else
Me.txtBillReceived.BackColor = lngRed
End If

End Sub
 
S

Steve Schapel

Amy,

I think that code should work, as long as the Back Style property of the
textbox isn't set to Transparent. However, you may want it on the
form's Load event rather thant the Open event, and presumably you will
also need to put it on the After Update event of the textbox itself.

A simpler approach would be to use Conditional Formatting (select the
textbox in Design View of the form, and then select Conditional
Formatting from the Format menu).
 
J

JethroUK©

if you want this to change to reflect each record - you need to include it
in the Form_current event
 
F

fredg

I am trying to set my program up so on a form if a field the user needs to
fill in is null then it will appear yellow and if it is filled like it should
be then red backcolor. I don't want to set them up as required fields, just
reminding the user these fields are suggested before printing. I have been
trying to use the below code and it just sits there no error codes or
anything. I have placed the code on the onOpen event for the form.Any help
would be greatly appreciated. I don't have experience with IsNull.Thank you

Dim lngYellow As Long
Dim lngRed As Long

lngYellow = RGB(255, 255, 0)
lngRed = RGB(255, 0, 0)

If IsNull(Me.txtBillReceived) Then
Me.txtBillReceived.BackColor = lngYellow
Else
Me.txtBillReceived.BackColor = lngRed
End If

End Sub

A simpler coding would be:

Private Sub txtBillReceived_AfterUpdate()
If IsNull(Me!txtBillReceived) Then
Me.txtBillReceived.BackColor = vbYellow
Else
Me.txtBillReceived.BackColor = vbRed
End If
End Sub

You can place the code in both the Form's Current event as well as the
txtBillReceived control's AfterUpdate event.
This will only work on a Single Form view form.

A better solution (if you are using Access 2000 or newer) would be to
use the txtBillReceived control's Conditional Formatting property.

Select the txtBillReceived control.
Set the regular Back Color (of the control) to Red.
Then click on Format + Conditional Formatting
Set Condition1 to
Expression Is
Write:
[txtBillReceived] Is Null
in the next box.

Set the BackColor to Yellow

The above will work in all form views.
No additional coding needed.
 
A

amy14775

Thank you all for the help. All responses helped me. I appreciate the time
and energy.
Thank you,
Amy
 

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