PC Review


Reply
Thread Tools Rate Thread

Comparing dates driving me crazy

 
 
JerryC
Guest
Posts: n/a
 
      7th Mar 2005
NDD = Format(Me![NextDueDate], "Short Date")
PMRange = 8
TD = Format(Date, "Short Date")

If NDD > DateAdd("d", PMRange * 2, TD) = True Then
MsgBox "Do This"
End If

Why is If statment always true ???

Thanks for your input.
 
Reply With Quote
 
 
 
 
Ken Snell [MVP]
Guest
Posts: n/a
 
      7th Mar 2005
Assuming that you didn't Dim NDD as a date variable, it's likely a variant
variable. When you set it equal to the result of a Format statement, you'll
get a string.

Thus, you're always testing a string against a date value in the If
statement. Because they're not the same data type, ACCESS will change one to
match the other, and I'm guessing that it is changing the result of the
DateAdd function to a string. As such, the > operator will test based on
string comparison, not based on date value comparison.

Change this line
NDD = Format(Me![NextDueDate], "Short Date")

to this
NDD = DateValue(Me![NextDueDate])

I also would add
Dim NDD As Date

to the procedure's code.
--

Ken Snell
<MS ACCESS MVP>

"JerryC" <(E-Mail Removed)> wrote in message
news:550101c5233b$f553a730$(E-Mail Removed)...
> NDD = Format(Me![NextDueDate], "Short Date")
> PMRange = 8
> TD = Format(Date, "Short Date")
>
> If NDD > DateAdd("d", PMRange * 2, TD) = True Then
> MsgBox "Do This"
> End If
>
> Why is If statment always true ???
>
> Thanks for your input.



 
Reply With Quote
 
JerryC
Guest
Posts: n/a
 
      7th Mar 2005
Thanks Ken, the DIM NDD As Date solved the problem



>-----Original Message-----
>Assuming that you didn't Dim NDD as a date variable,

it's likely a variant
>variable. When you set it equal to the result of a

Format statement, you'll
>get a string.
>
>Thus, you're always testing a string against a date

value in the If
>statement. Because they're not the same data type,

ACCESS will change one to
>match the other, and I'm guessing that it is changing

the result of the
>DateAdd function to a string. As such, the > operator

will test based on
>string comparison, not based on date value comparison.
>
>Change this line
> NDD = Format(Me![NextDueDate], "Short Date")
>
>to this
> NDD = DateValue(Me![NextDueDate])
>
>I also would add
> Dim NDD As Date
>
>to the procedure's code.
>--
>
> Ken Snell
><MS ACCESS MVP>
>
>"JerryC" <(E-Mail Removed)> wrote in

message
>news:550101c5233b$f553a730$(E-Mail Removed)...
>> NDD = Format(Me![NextDueDate], "Short Date")
>> PMRange = 8
>> TD = Format(Date, "Short Date")
>>
>> If NDD > DateAdd("d", PMRange * 2, TD) = True Then
>> MsgBox "Do This"
>> End If
>>
>> Why is If statment always true ???
>>
>> Thanks for your input.

>
>
>.
>

 
Reply With Quote
 
David C. Holley
Guest
Posts: n/a
 
      8th Mar 2005
Couple of thoughts (working off of 4 hours of sleep, facing a 16 hour
day)...

It shouldn't be neccessary to use the Format() function to compare two
date values. As long as the values are encapsulated in ## the compare
should work. Use IsDate() to confirm that [NextDueDate] is a date, if
not, use DateSerial() or CDate() to convert it. As to debuging, snoop
around the code by replacing the variable with actual values, this will
let you know if the problem is with the logic or somethign with the
variables. And of course DEBUG.PRINT

JerryC wrote:
> NDD = Format(Me![NextDueDate], "Short Date")
> PMRange = 8
> TD = Format(Date, "Short Date")
>
> If NDD > DateAdd("d", PMRange * 2, TD) = True Then
> MsgBox "Do This"
> End If
>
> Why is If statment always true ???
>
> Thanks for your input.

 
Reply With Quote
 
David C. Holley
Guest
Posts: n/a
 
      8th Mar 2005
Wouldn't it be better to DIM NDD as Variant to allow [NextDueDate] to be
null and then test for null? I've had several instances where I declared
a function as a particular type only to pass it a NULL value and have it
crap out.

Ken Snell [MVP] wrote:
> Assuming that you didn't Dim NDD as a date variable, it's likely a variant
> variable. When you set it equal to the result of a Format statement, you'll
> get a string.
>
> Thus, you're always testing a string against a date value in the If
> statement. Because they're not the same data type, ACCESS will change one to
> match the other, and I'm guessing that it is changing the result of the
> DateAdd function to a string. As such, the > operator will test based on
> string comparison, not based on date value comparison.
>
> Change this line
> NDD = Format(Me![NextDueDate], "Short Date")
>
> to this
> NDD = DateValue(Me![NextDueDate])
>
> I also would add
> Dim NDD As Date
>
> to the procedure's code.

 
Reply With Quote
 
Ken Snell [MVP]
Guest
Posts: n/a
 
      8th Mar 2005
While a variant variable declaration would enable the variable to hold a
Null value, I (and others) would not recommend that you use Variant just for
that reason. If the intended data to be held in that variable is date/time,
then you should declare it as Date. That avoids any confusion in the code
and the calculations/processing regarding what the data type actually is.

If one suspects that a Null value might be an issue, then the code can use
the Nz function to capture it and to handle it as the programmer intends.

--

Ken Snell
<MS ACCESS MVP>


"David C. Holley" <(E-Mail Removed)> wrote in message
news:Ov%(E-Mail Removed)...
> Wouldn't it be better to DIM NDD as Variant to allow [NextDueDate] to be
> null and then test for null? I've had several instances where I declared a
> function as a particular type only to pass it a NULL value and have it
> crap out.
>
> Ken Snell [MVP] wrote:
>> Assuming that you didn't Dim NDD as a date variable, it's likely a
>> variant variable. When you set it equal to the result of a Format
>> statement, you'll get a string.
>>
>> Thus, you're always testing a string against a date value in the If
>> statement. Because they're not the same data type, ACCESS will change one
>> to match the other, and I'm guessing that it is changing the result of
>> the DateAdd function to a string. As such, the > operator will test based
>> on string comparison, not based on date value comparison.
>>
>> Change this line
>> NDD = Format(Me![NextDueDate], "Short Date")
>>
>> to this
>> NDD = DateValue(Me![NextDueDate])
>>
>> I also would add
>> Dim NDD As Date
>>
>> to the procedure's code.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET driving me crazy shapper Microsoft ASP .NET 2 14th Nov 2007 09:11 PM
Help its driving me crazy =?Utf-8?B?c2Fzc3lr?= Windows XP Help 1 18th Apr 2007 03:33 AM
POP UPS ARE DRIVING ME CRAZY =?Utf-8?B?U1RBQ0lF?= Windows XP Help 3 10th Sep 2004 02:41 PM
IE 6.0 is driving me crazy! Precious Windows XP Internet Explorer 0 15th Jan 2004 05:20 AM
pop up ads driving me crazy papaver Windows XP Performance 2 4th Jul 2003 05:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:11 AM.