Working with >

J

John

Can someone explain why this works:

i = DateDiff("d", dBackUp, Now)
If i > 30 Then

and this:

If DateDiff("d", dBackUp, Now) > 30 then

doesn't?

Thanks.
John
 
S

Steve Schapel

John,

Can you please give a specific example? Or otherwise explain what you
mean by "doesn't work"? Thanks.
 
R

Rob Parker

Works for me (Access 2002):

Does this work for you?

Public Sub IfTest()
Dim d1 As Date, d2 As Date
Dim i As Integer

d1 = #1/1/2008#
d2 = Date

i = DateDiff("d", d1, d2)
If i > 30 Then
MsgBox "Assigned to variable - works"
End If
If DateDiff("d", d1, d2) > 30 Then
MsgBox "Datediff in comparison - works"
End If
End Sub

It also works if I assign d2 = Now (without changing the type declaration of
i - which should not be required since DateDiff returns an integer)

Rob
 
J

John

Steve,

If the outcome of DateDiff("d", dBackUp, Now) is greater than 30 then the
code:

If DateDiff("d", dBackUp, Now) > 30 then
msgbox "blabla"
endif

doesn't show the "blabla".
When I use the i variable as shown below it does show me the "blabla".

John
 
J

John

Thanks Rob, Steve,

Yes this works for me and my own code works now as well...
I think I should get some sleep...;-)

John
 
S

Steve Schapel

John,

It has occurred to me that It just may have been an isolated incident
related to the use of Now() function. Depending on the dates you were
comparing. The Now() function includes a time component. So the
difference between Now() and a date 30 days ago is more than 30 days, if
you catch my drift. Whereas your other example, casting the type as
Integer, would remove the time component, making the comparison an exact
number of days.
 
J

John

Thanks for the insight.
John

Steve Schapel said:
John,

It has occurred to me that It just may have been an isolated incident
related to the use of Now() function. Depending on the dates you were
comparing. The Now() function includes a time component. So the
difference between Now() and a date 30 days ago is more than 30 days, if
you catch my drift. Whereas your other example, casting the type as
Integer, would remove the time component, making the comparison an exact
number of days.
 
Top