FormatDateTime with CDate wrapper not returning Date data type in make table query
I have this line of code in my Access Make-Table query and it returns the right value, however, when i open the table it creates and look at the properties, all my date fields are formatted as Text. Anyone know why this might be?
LA: IIf(Item001_AllFilesPresent![Last Accessed] Is Not Null,(CDate(FormatDateTime(Item001_AllFilesPresent![Last Accessed],1))),"")
Microsoft AccessSQL
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Instead of using a make-table query, where Access often assumes (get it, ass-u-me) the data types for the various columns, have the table pre-defined with the column types you want, and then just run a delete query followed with an append query.
Yaniv Schiff
ASKER
Thanks, that worked. Just so i understand, it was the "" that was screwing it up because access considers that text?
HOWEVER, Expr1 only works if the value isn't null because of the FormatDateTime function.
But if CVDATE can format the date/time automatically to only display the date, then it will work.
CVDate only converts from something to DateTime data type, much like CDate, but it does not convert the value.
CDate does not accept Null which CVDate does.
FormatDateTime can be dangerous as it returns localised expressions.
A better choice would be to use Format(value, "m\/d\/yyyy") but it will return an empty string for Null.
To cut the time part, Fix can be used and it accepts and returns Null as well.
Thus, to return either Null or a DateTime value without the time part:
Localised means in local language as to the settings of the machine. If that language is not English in one of its varieties, it may cause troubles in code.
The main purpose of FormatDateTime is for display for human reading.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Isn't CVDate deprecated now ?
From Help:
"A CVDate function is also provided for compatibility with previous versions of Visual Basic. The syntax of the CVDate function is identical to the CDate function, however, CVDate returns a Variant whose subtype is Date instead of an actual Date type. Since there is now an intrinsic Date type, there is no further need for CVDate. The same effect can be achieved by converting an expression to a Date, and then assigning it to a Variant. This technique is consistent with the conversion of all other intrinsic types to their equivalent Variant subtypes."
mx
Gustav Brock
Not quite. It is still here in Access 2007 and it is documented.
And it is extremely useful in some cases. We've just seen one example.
/gustav
Yaniv Schiff
ASKER
That makes sense. I was wondering why my formatting was getting screwed up depending on which machine i was running the database. Thanks. This post has been very informative!!
Cactus data has it right in this case. When i run the function with CDate i get an error when the values are null, when i run it with CVDate, no errors.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Well, I wasn't questioning whether he was right, only what it says in the Help File regarding CVDate() ... and that "The same effect can be achieved by ......".
LA: CVDate([Item001_AllFilesPr
/gustav