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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare date field with now() and run If statement 1

Status
Not open for further replies.

cdgeer

IS-IT--Management
Joined
Apr 8, 2008
Messages
133
Location
US
I can't seem to get the syntax right in VB. Still a newby. I'm simply trying to compare the date in a text box on a form with the current date and run an If Then statement to change the Font color property of the text box.
This is what I have now although I've tried many, many other variations.

Private Sub Form_Open(Cancel As Integer)

Dim Today
Dim Due

Today = DateValue(Now())
Due = DateValue(Me.AuditDue)

If Today > Due Then Me.AuditDue.ForeColor = 255

End Sub
 
Use the Load event instead of the Open.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I just tried it. The date field stays red (ForeColor=255)
no matter what date is in the field. I tried putting in

Else Me.AuditDue.ForeColor = 0
but I get the error... "Else without If"
also when I debugged a couple times I noticed that "Due" was not returning a value. Is the If statement wrong maybe?
 

A couple of things you can check:

1. Does your text box have the same name as the field?
2. Today is a reserved word.
3. You're declaring your variables as variants. Perhaps
Code:
Dim dtToday As Date
Dim dtDue As Date
dtToday = DateValue(Now())
dtDue = DateValue(Me.AuditDue)
If dtToday > dtDue Then
   Me.AuditDue.Forecolor = 255
Else
   Me.AuditDue.Forecolor = 0
End If

Randy
 
Yes, textbox is named AuditDue. I tried suggestions above and then some. Still ForeColor is always red(255). No matter what the date in field is. I'm Stumped!
 
And what about the Current event procedure ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm not familiar with it. Do you mean CurrentRecord?
How would I incorporate it into what I'm trying to do?
 
Do you mean CurrentRecord
No, I mean the Current event of the Form.
 
Thanks so much PHV! I've never used Current event before so I had to look up. I used the following code and it works perfect. Thanks Again!!

Private Sub Form_Current()
If DateDiff("d", Me.AuditDue, Date) > 0 Then
Me.AuditDue.ForeColor = 255
Else: Me.AuditDue.ForeColor = 0

End If

End Sub
 

I've inserted your exact code into the Form_Open event and it responds as expected. With 02/01/2009 in the AuditDue field, the font is red. With 02/01/2010 in the Audit Due field, the font is black. Perhaps you should try to recreate the form?


Randy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top