Solved DateDiff() (1 Viewer)

PatAccess

Registered User.
Local time
Today, 18:49
Joined
May 24, 2017
Messages
284
Hi Guys,
I am trying to use the DateDiff() in a VBA code which will change text colors base on the result. My issue is I am a little confused as how to I say:
If date is between 30 and 60 days late (so -30 and -60 right?) make it red
If date is 30 days then make it blue but if it is in the future make it black.
Here my test code but it makes all text black so something is off. Can you help me please?
Code:
Private Sub Command4_Click()
Dim intDate As Integer
Dim date1 As Date
Dim date2 As Date

date1 = Date
date2 = Me.Expires
intDate = DateDiff("d", date1, date2)

If intDate > -60 And intDate < -30 Then
    Me.Expires.ForeColor = vbRed
ElseIf intDate > -30 And intDate < 0 Then
    Me.Expires.ForeColor = vbBlue
Else
    Me.Expires.ForeColor = vbBlack
End If

End Sub

Thank you
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:49
Joined
Sep 21, 2011
Messages
14,366
Walk through the code with F8 and see what it does.
Just on the face of it, it appears fine. Myself, I would swap the dates and just use positive values, but that is me.
However walking through the code and hovering over the variables will show you where you are going wrong.?

Basic debugging steps I know, but they do solve a lot of 'cannot see what is wrong' problems. :)
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:49
Joined
Oct 29, 2018
Messages
21,498
Also, I would probably use a Case Select statement, but that's just me. :)
 

PatAccess

Registered User.
Local time
Today, 18:49
Joined
May 24, 2017
Messages
284
Walk through the code with F8 and see what it does.
Just on the face of it, it appears fine. Myself, I would swap the dates and just use positive values, but that is me.
However walking through the code and hovering over the variables will show you where you are going wrong.?

Basic debugging steps I know, but they do solve a lot of 'cannot see what is wrong' problems. :)
Yes stepping through really did help see what I was getting and I also switched the date as you've suggested and it worked better. Thank you so much
 

Users who are viewing this thread

Top Bottom