473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dateadd criteria problem

Hi there, im hoping someone might be able to painlessly tell me what im incorrectly doing here/expecting.

Im using Access 2007

I am creating a query to show ongoing tasks in a database.
in the fields I have
Start date (dd/mm/yyyy)
Duration (months)

Basically i want the query to calculate is if start date + Duration is greater than today...

so ive used
Dateadd("m",[duration],[start date])

this is great up to this point - it will show on running the finish date in the format dd/mm/yyyy

but when I add the criteria
>date()

I get the error message "data type mismatch in criteria expression"

I think the criteria is formed correctly - as it works on the start date field independently.
Infact if i put any criteria in the field that the dateadd is present i get the same message.

Basically - are the two conditions - dateadd of two fields and criteria functions compatible?
If not is there any other way i could combat the problem?

regards
Mar 16 '08 #1
20 7448
Scott Price
1,384 Expert 1GB
Try enclosing your start date in # marks within the DateAdd statement.

As a test I wrote these two statements in the Immediate window:

Expand|Select|Wrap|Line Numbers
  1. ?DateAdd("m", 1, 15/3/2008)>Date()
  2. ?DateAdd("m",1,#15/3/2008#)>Date()
  3.  
The first evaluates false, the second evaluates true. What is happening with these two statements is that vba attempts to change the variant returned by the DateAdd into a recognizable date, and for the first it comes up with something in the year 1900. This obviously is not greater than today :-) Enclosing in hash marks (#) makes it correctly evaluate.

I do find it strange that you are getting a type mismatch error, though...

Regards,
Scott
Mar 16 '08 #2
Scott Price
1,384 Expert 1GB
After doing some more testing (using Access 2003) I'm starting to wonder what really is happening here.

Doing what you are trying to do works perfectly well for me without adding # marks or anything else. I recreated as best I could in my test database, but could not reproduce the error.

Check to make sure that the startdate is being stored as a Date/Time value, not a Text value. Please also post the sql for your query for us. After pasting the sql into the reply window, please enclose it in the code tags by selecting the sql text and clicking the # icon on the top of this reply window.

Thanks!

Regards,
Scott
Mar 16 '08 #3
Hi there,

Thaks for your help so far.. Im gonna be honest my SQL knowledge - you could write on a postage stamp...

Here is the Sql code from my query where it calculates the end date accurately

Expand|Select|Wrap|Line Numbers
  1. SELECT [Tbl Projects].[Projects Code], [Tbl Projects].[Project Type], [Tbl Projects].[Region], [Tbl Projects].[Start Date], [Tbl Projects].[Duration (mths)], [Tbl Projects].[Cost], DateAdd("m",[Duration (mths)],[Start Date]) AS [end]
  2. FROM [Tbl Projects];
here is the same query with me adding the criteria that causes the error...

Expand|Select|Wrap|Line Numbers
  1. SELECT [Tbl Projects].[Projects Code], [Tbl Projects].[Project Type], [Tbl Projects].Region, [Tbl Projects].[Start Date], [Tbl Projects].[Duration (mths)], [Tbl Projects].Cost, DateAdd("m",[Duration (mths)],[Start Date]) AS [end]
  2. FROM [Tbl Projects]
  3. WHERE (((DateAdd("m",[Duration (mths)],[Start Date]))>Date()));
the start date field is date/time type and the duration (mths) field is a number.

Thanks again
Mar 16 '08 #4
Scott Price
1,384 Expert 1GB
Open your query in design view and in the Criteria space below the Field where you have
Expand|Select|Wrap|Line Numbers
  1. end: DateAdd("m", [Duration (mths)], [Start Date])
add the
Expand|Select|Wrap|Line Numbers
  1. >Date()
.

The sql will look nearly the same, if not exactly the same, but it throws an error when attempting to use the whole DateAdd() >Date() as a WHERE criteria.

Give this a try and let me know if it works.

Regards,
Scott
Mar 16 '08 #5
hiya,

yeah thats exactly what i was doing - so just tried and still gives me an error.
Mar 16 '08 #6
Scott Price
1,384 Expert 1GB
Very strange, since it works perfectly for me!

There are a few differences with the way A2003 and A2007 work, but I wasn't aware that this was one of the difficulties. Let me research a bit and I'll get back to you.

Regards,
Scott
Mar 16 '08 #7
Scott Price
1,384 Expert 1GB
Just for grins and chuckles, can you open the VBA editor window (Alt+F11 does the trick in A2003), then in the Immediate window (Ctl+G) type in
Expand|Select|Wrap|Line Numbers
  1. ?Date()
and hit enter.

Let me know what format the date is in: dd/mm/yyyy or mm/dd/yyyy.

Regards,
Scott
Mar 17 '08 #8
missinglinq
3,532 Expert 2GB
I thought of that too, Scott! It would really help if people at least listed their country of residence in their profiles, especially in Date related questions!

Linq ;0)>
Mar 17 '08 #9
Scott Price
1,384 Expert 1GB
Thanks Linq!

I noticed that in his first post he mentioned the dd/mm/yyyy format... However reading online about A2007, it says that Date() returns in mm-dd-yyyy...

Testing with A2003 I found that the Date() function returns in the format specified in the control panel settings.

Now I'm just wondering if A2007 changed this function to only return in one format. Hopefully not, but I can't think of any other situation that would give rise to the error he reports.

Regards,
Scott
Mar 17 '08 #10
Killer42
8,435 Expert 8TB
Surely the Date() function returns a Date value, not a formatted string. How it is subsequently formatted should be independent of where the value came from.
Mar 17 '08 #11
Scott Price
1,384 Expert 1GB
Well, according to Allen Browne the 'wonder from down under': International Dates in Access there is a potential problem using this type of date in a calculated field in a query. He recommends as a solution using the CVDate() as a wrapper for the whole date calculation in order to take care of potential nulls (cvdate is better than cdate for this) and to erase any ambiguity that Access could use to throw an error.

That would translate into:
Expand|Select|Wrap|Line Numbers
  1. end: CVDate(DateAdd("m", [Duration (mths)], [Start Date]))
Regards,
Scott
Mar 17 '08 #12
MMcCarthy
14,534 Expert Mod 8TB
Out of Curiousity can you substitute Now() for Date(). There have been some rumours of the Date() function not being fully supported in 2007.
Mar 17 '08 #13
Killer42
8,435 Expert 8TB
Out of Curiousity can you substitute Now() for Date(). There have been some rumours of the Date() function not being fully supported in 2007.
This certainly sounds like a more fruitful line of investigation, since as far as we know, the result of the DateAdd() was already being interpreted as a date value before, and only comparing it to Date() produced the mismatch.
Mar 17 '08 #14
NeoPa
32,556 Expert Mod 16PB
To illustrate whether or not the Date function is fully supported in A2007 SQL it may well be worth doing a test by substituting Now() for Date().

If this turns out to be the case however, that would be an extraordinarily retrograde step for Access. Very few people can manage to get the logic right using Now() when testing for dates as it is. Simply put, when testing raw dates (no time elements) the Date() function should ALWAYS be used, as using Now() will result in false logic.

As far as formats are concerned, Killer is right on the mark. The values returned from both the Date() and DateAdd() functions should be Date/Times and not formatted strings. This means that the format that they are displayed in is entirely irrelevant.

The format of date LITERALS in SQL however, is neither irrelevant nor dependent on your regional settings (See Literal DateTimes and Their Delimiters (#)). This is layed down in the SQL standards as m/d/y. Access is very forgiving in this respect, allowing all sorts of different date formats in SQL as long as they are not ambiguous. When ambiguous (EG 1/5/2008) it will always assume the SQL standard of m/d/y.
Mar 17 '08 #15
Scott Price
1,384 Expert 1GB
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:
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 "compatibility" issue highlighted in the Access documentation.)
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
Mar 17 '08 #16
right then,

Thank you ever so much for your help thus far..

I can report..

the date format from visual basic is dd/mm/yy
Im in the UK
using Access 07

On a new database - the CVdate thing works with a criteria. so that appears to be that problem solved! Cheers for that!

however on the database i'm working on it doesnt ("rolls eyes") - i can only presume ive picked up an error somewhere etc.. or knocked an option somewhere.. so im going to spend the next few days figuring where the two DB's differ. They are idnetical in the table design and their properties as far as i can tell.
Mar 17 '08 #17
Scott Price
1,384 Expert 1GB
After the usual backup routine do a Compact/Repair of the database. Make sure all your code changes are compiled also. Hope you find the problem!

Good luck,

Regards,
Scott
Mar 17 '08 #18
NeoPa
32,556 Expert Mod 16PB
@Scott It is worth bearing in mind that MS describe the CVDate() function as legacy functionality, to support projects designed on older systems.

I mention this merely to warn that it may disappear on you in a future version, though probably not any time soon ;)
Mar 18 '08 #19
Scott/NeoPa

Thank you both for your wisdom over the last few days

I feel quite sheepish now though, the final step that fixed the error on my database in the query was the compact/repair.. (that is using the CVdate)

I did say i was a bit of a novice.. but im a little frustrated i didnt even cotton onto a menu command...

hopefully this will be of some use to someone else though

Thanks
Mar 18 '08 #20
Scott Price
1,384 Expert 1GB
No need to feel sheepish, Anthony! Glad we could get it ironed out for you fairly painlessly :-)

My general rule of thumb is to Compact/Repair at the end of every day when developing a database. That's in addition to Compile & Save after ANY code changes. Access databases can get corrupted from any number of causes, but are especially vulnerable during the development phase while adding, testing, debugging and changing code. These steps help to keep the corruption bug at bay.

Good luck on the rest of the app!

Regards,
Scott
Mar 18 '08 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Brandon | last post by:
Greetings, I am having a problem getting a SQL statement that is functional in Access 2K to work in ASP. My Access SQL statement is this: SELECT DateAdd('s',Ticket_Opened_Date,'Dec 31, 1969...
1
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN...
15
by: PMBragg | last post by:
Thank you in advance. I'm trying to pull all inventory items from December of the previous year back to 4 years for my accountant. I know this can be done, but I'm drawing a blank. I've tried; ...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
1
by: No Spam | last post by:
Access 2000, W2K I am using a DSUM formula in one of my report fields to calculate a dollar amount. When the user opens the report, they are prompted with a box requesting Period End Date. The...
3
by: Annette Massie | last post by:
I am trying to insert a record into a table via code and one of the values to add I would like as a dateadd calculation on a value from a query. My code looks like this: Set db = CurrentDb() ...
4
by: ey.markov | last post by:
Greetings, I have an A2K application where for a report the user enters a month-end date, and the system must gather transactions for that month. No problem, I thought, I'll just use the DateAdd...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
2
by: awojciehowski | last post by:
Can any one point me in the right direction here... I have a report that shows an entry and under that record there are several sub records...best way to explain it is imagine an order with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.