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

Best way to check a Date variable? 1

Status
Not open for further replies.

Sheco

Programmer
Jan 3, 2005
5,457
US
Is it better to use DateValue() to check to see if a date variable has been set?
Code:
Function DateIsSet(dtMyDate As Date) as Boolean
  If (DateValue(dtMyDate) <> 0) Then DateIsSet= True
End Function

Or perhaps to compare it with date variable that you know has not been set?
Code:
Function DateIsSet(dtMyDate As Date) as Boolean
  Dim dtFake as Date
  If (dtMyDate <> dtFake) Then DateIsSet= True
End Function

Or is there some better way?
 
I'm pretty sure that an uninitialized Date variable has a value of 12/31/1899

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
So this would be better?
Code:
Function DateIsSet(dtMyDate As Date) As Boolean
  DateIsSet = dtMyDate <> CDate("12/30/1899")
End Function
 

A date is a dp number.
Try:

If Cdbl(dtMyDate)= 0
(which = 1899-12-30

However, if the variable holds time, but not a date, and you want to only check if a date has been set, then:

If Int(DateValue(dtMyDate)) = 0
(instead of DateValue, you could use Int())

Please not: Do not use this if the date could possibly hold a value of 30 dec 1899 (= 0)
 
Simple compare the date variable with 0.
___
[tt]
Dim D As Date
MsgBox D = 0 'uninitialized, True
D = Now
MsgBox D = 0 'initialized, False[/tt]
 


>If Int(DateValue(dtMyDate)) = 0
>(instead of DateValue, you could use Int())

Sorry, typo:

If DateValue(dtMyDate) = 0
(instead of DateValue, you could use Int())

Anyways, you could also just use:

If dtMyDate = 0 Then

Just as a numeric variable gets initialized to a value of 0, or a string to "", a date variable gets initialized to zero, which translates to 1899-12-30 00:00:00.
So, there is not other real way of checking it it was already set to a valid date by code or user input.
The lesser elegant method would be to initialize all of your variables to a unrealistic date (100 AD), or use a variant (not recommended, except inside small preceedures, unless you can keep good tabs on it and be sure the variable doesn't get set to a wrong value, or even an object)

I usually use Strings and use CDate and IsDate() on them, but only if the possibility exits that the variable may not contain a date = "".
Then you can assign a db date field to it, and if it doesn't have a date (null) then the string is = vbNullString. A little extra work, but when you get into the habbit of it, works out good.

 

(Sorry - Didn't see Hypetia's post prior to my posting)

But, the zero is also a date, so it is already initialized. Just remember that in case you might be working with dates from the 19th century (realestate!)
 

>or a string to ""

That was wrong for a string - don't know what I was thinking
 
I never liked the way the unitialized date shows up as "12:00:00 AM"
Code:
Dim dtFake as Date
MsgBox dtFake
 
That depends on how the date format is set in the system.
I have 24 hour time and it shows up as 00:00:00

But, a date variable with a set date and now a date-time, as when using Now(), always has that time value - it just doesn't show up until you use:

dtFake=Date
?TimeValue(dtFake)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top