The Date() function returns a Date/Time value which in Access is stored behind the scenes as an Integer. The DateAdd() function returns a Variant with subtype Date. Theoretically it will never be able to return an invalid date value, however, one never knows with new software releases!
To quote the relevant paragraph from Allen Browne:
This sounds to me exactly what the OP is experiencing... Because the DateAdd function returns a Variant, it's possible (probable) that when it doesn't recognize the resulting value as a Date subtype, it casts it as a String subtype instead, thus resulting in the Data Type mismatch error.
Using the CVDate() wrapper therefore correctly coerces the Variant returned from DateAdd() into a Variant with Date subtype.
Regards,
Scott
To quote the relevant paragraph from Allen Browne:
DATA TYPE NOT RECOGNIZED: Calculated Date Fields
Access can also misinterpret calculated date fields in queries, especially where the date format is not American and the field contains some Nulls. The obvious symptoms are that the field is left-aligned and sorted as strings.
The solution is to explicitly typecast all calculated date fields, e.g.:
DueDate: CVDate([InvoiceDate] + 30)
(Note: CDate() fails on Null values, so CVDate() has more uses than the "compatibil ity" issue highlighted in the Access documentation.)
Access can also misinterpret calculated date fields in queries, especially where the date format is not American and the field contains some Nulls. The obvious symptoms are that the field is left-aligned and sorted as strings.
The solution is to explicitly typecast all calculated date fields, e.g.:
DueDate: CVDate([InvoiceDate] + 30)
(Note: CDate() fails on Null values, so CVDate() has more uses than the "compatibil ity" issue highlighted in the Access documentation.)
Using the CVDate() wrapper therefore correctly coerces the Variant returned from DateAdd() into a Variant with Date subtype.
Regards,
Scott
Comment