[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Datediff results in criteria expression

Posted on 2012-08-23
34
Medium Priority
?
879 Views
Last Modified: 2012-08-24
I have an Access query in which I have an expression that calculates the number of months since a person's employment was verified by using the DateDiff function.  The field that contains the date of the employment verification is named "EMPLOYMENT_DATE".  The expression that determines the number of months since the last verification is:

MonthsSinceEmploymentVerification: DateDiff("m",[EMPLOYMENT_DATE],Date())

This returns what looks like an integer value, and the values appear correct.  The problem is that I want to see only those records where the number of months is >6.  If I put >6 on the criteria line of the query I get a data type mismatch error.  I have tried a variety of things including wrapping EMPLOYMENT_DATE in DateValue(EMPLOYMENT_DATE), and although that appears to produce the same integer value, I can't put the criteria on it either without throwing the data type mismatch error.  

I have read the various postings in the knowledge base, but have not found anything that addresses this directly,  
Thanks in advance for your help.
Ron
0
Comment
Question by:rfreud
  • 15
  • 14
  • 3
  • +2
34 Comments
 
LVL 12

Expert Comment

by:serchlop
ID: 38326686
I don't know if could help to use val() function



MonthsSinceEmploymentVerification: val(DateDiff("m",[EMPLOYMENT_DATE],Date()))
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326714
is Employment_Date coming in as a DATE/TIME? or STRING?
0
 

Author Comment

by:rfreud
ID: 38326720
Tried your suggestion and got the same error.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:rfreud
ID: 38326727
It is coming from a string.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326762
that could be your problem your problem.

do this:
DateDiff("m",DateValue ([EMPLOYMENT_DATE]),Date())

Then put in >6 in criteria.


if that fails, we can do the stronger Date covnersion, but because it's a string, it could be coming back in the wrong value set.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326771
Here's a stronger string to date conversion:
cdate(format(mystringFieldThatIsInYYYYMMDDFormat, "####/##/##"))
0
 

Author Comment

by:rfreud
ID: 38326786
I tried this, and still got the same error.  I then tried creating a date value for EMPLOYMENT_DATE using this expression:
DateValueOfEmploymentDate: DateValue([EMPLOYMENT_DATE])    and then substituting
DateValueOfEmploymentDate in the original datediff expression.  

This resulted in Access asking for the Parameter Value for DateValueOfEmploymentDate
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326827
can you please post your query in whole again?
0
 

Author Comment

by:rfreud
ID: 38326838
Not sure I know what you mean, "in whole"...
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326845
THis:
MonthsSinceEmploymentVerification: DateDiff("m",[EMPLOYMENT_DATE],Date())

What does it look like now?
0
 

Author Comment

by:rfreud
ID: 38326863
MonthsSinceEmploymentVerification: DateDiff("m",[DateValueOfEmploymentDate],Date())

I created DateValueOfEomploymentDate this way:  
DateValueOfEmploymentDate: DateValue([EMPLOYMENT_DATE])
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326872
Try this:
MonthsSinceEmploymentVerification: DateDiff("m",DateValue([EMPLOYMENT_DATE]),Date())
0
 

Author Comment

by:rfreud
ID: 38326887
Got the same data type mismatch error.  If I take off the crieteria >6 and run the query it again runs fine and appears to deliver integer values for MonthsSinceEmploymentVerification.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38326893
ok...can you please send me a screenshot of your entire query window?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38327025
As a shot in the dark, try it like this perhaps:

MonthsSinceEmploymentVerification: clng(DateDiff("m",[EMPLOYMENT_DATE],Date()))
0
 

Author Comment

by:rfreud
ID: 38327099
Nope - got the same data type mismatch error!
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327111
This maybe part of a relationship with the rest of the query.
Can you send over your SQL?
0
 

Author Comment

by:rfreud
ID: 38327134
Here is what I get when I show the SQL:

SELECT ALD.POOLNUMBER, ALD.EMPLOYMENT, ALD.EMPLOYMENT_DATE, DateValue([EMPLOYMENT_DATE]) AS DateValueOfEmploymentDate, CLng(DateDiff("m",[EMPLOYMENT_DATE],Date())) AS MonthsSinceEmploymentVerification
FROM ALD
WHERE (((ALD.EMPLOYMENT)="Yes" Or (ALD.EMPLOYMENT)="Y") AND ((ALD.EMPLOYMENT_DATE) Is Not Null And (ALD.EMPLOYMENT_DATE)<>"nAV") AND ((CLng(DateDiff("m",[EMPLOYMENT_DATE],Date())))>6))
ORDER BY ALD.POOLNUMBER;
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327143
EMPLOYMENT_DATE IS A STRING, right?
Can you please tell me the format of the string?
0
 

Author Comment

by:rfreud
ID: 38327161
Here is a screen capture of the EMPLOYMENT_DATE field definition in the table.  I just noticed the "@" sign in the "format" characteristic, and don't know what that is.  Maybe that's getting in the way?  This information was imported from an Excel spreadsheet, if that helps shed some light on this.
Employment-String-Capture.JPG
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327167
what's the format of the text string?
How is the date represented?
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327173
Try This:

SELECT ALD.POOLNUMBER, ALD.EMPLOYMENT, DateDiff("m",[EMPLOYMENT_DATE],Date()) AS MonthsSinceEmploymentVerification
FROM ALD
WHERE (((ALD.EMPLOYMENT)="Yes" Or (ALD.EMPLOYMENT)="Y") AND ((DateDiff("m",[EMPLOYMENT_DATE],Date()))>6))
ORDER BY ALD.POOLNUMBER;
0
 

Author Comment

by:rfreud
ID: 38327205
Still have the same error.  BTW, I opened up a fresh new query in design view and pasted your syntax into it (after adding the ALD table to the upper pat of the grid.  So I don't think that it is some sort of corruption of the object.
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327217
ok...there's a piece missing here.  THis should work.

Please send me an example out put of that table.
New-Microsoft-Access-Database.accdb
0
 

Author Comment

by:rfreud
ID: 38327250
I opened your sample (thank you!!)  and when I ran the query with the rows you have there, it ran just fine.  When I added a record and noted the EMPLOYMENT_DATE as "NAV" and ran the query, it produced the same "data mismatch error".

Time for dinner now, and while I am tremendously grateful for your help, I don't think I will be able to continue this discussion throughout the evening.  Can we pick this back up in the AM?

Thanks,
Ron
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38327255
no problem.

When you get back...please explain "AS NAV"
I didn't see that in your SQL:

SELECT ALD.POOLNUMBER, ALD.EMPLOYMENT, ALD.EMPLOYMENT_DATE, DateValue([EMPLOYMENT_DATE]) AS DateValueOfEmploymentDate, CLng(DateDiff("m",[EMPLOYMENT_DATE],Date())) AS MonthsSinceEmploymentVerification
FROM ALD
WHERE (((ALD.EMPLOYMENT)="Yes" Or (ALD.EMPLOYMENT)="Y") AND ((ALD.EMPLOYMENT_DATE) Is Not Null And (ALD.EMPLOYMENT_DATE)<>"nAV") AND ((CLng(DateDiff("m",[EMPLOYMENT_DATE],Date())))>6))
ORDER BY ALD.POOLNUMBER;
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 38328151
This will work:

SELECT
  ALD.POOLNUMBER,
  ALD.EMPLOYMENT,
  ALD.EMPLOYMENT_DATE,
  DateValue([EMPLOYMENT_DATE]) AS DateValueOfEmploymentDate,
  DateDiff("m",DateValue([EMPLOYMENT_DATE]),Date())) AS MonthsSinceEmploymentVerification
FROM
  ALD
WHERE
  (ALD.EMPLOYMENT="Yes" Or ALD.EMPLOYMENT="Y")
  AND
  IsDate(ALD.EMPLOYMENT_DATE) = True
  AND
  DateDiff("m",DateValue([EMPLOYMENT_DATE]),Date()) > 6
ORDER BY
  ALD.POOLNUMBER;

/gustav
0
 

Author Comment

by:rfreud
ID: 38329151
Good morning -
This comment addresses both arcee123 and cactus_data's comments.

As to the "NAV" question:  I added a record to the sample database file you prepared.  I entered "Y" in  the EMPLOYMENT column and "NAV" in the EMPLOYMENT_DATE column (see attached file).  In the attached file I have labeled the two queries with the names of the contributors (both arcee123 and cactus_data).

As to cactus-data's solution, I pasted the SQL statement in and when I ran the query it gave me and error explaining that there was an extra parenthesis in the syntax, which was located just after the date().  I removed it and when I ran the query it still gave me the same data type mismatch in criteria expression error.  

I am on the east coast and at 1 PM eastern time I need to go to a meeting that will last until 4 PM eastern time, so I will be out of pocket during those hours.  Otherwise I am available to work on this with you and I REALLY appreciate your assistance.
Thanks,
Ron
Expert-Solution.accdb
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38329309
AAAAHHHHH.....
You put the value "NAV" in the employment_Date column.....
This is why it is failing.

ok.  Is NAV the only value you use besides dates?
if not, what else goes into that column?
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 38329346
OK, here it is:

SELECT
  ALD.POOLNUMBER,
  ALD.EMPLOYMENT,
  ALD.EMPLOYMENT_DATE,
  DateValue([EMPLOYMENT_DATE]) AS DateValueOfEmploymentDate,
  DateDiff("m",IIf(IsDate([EMPLOYMENT_DATE]),DateValue([EMPLOYMENT_DATE]),Date()),Date()) AS MonthsSinceEmploymentVerification
FROM
  ALD
WHERE
  (((ALD.EMPLOYMENT)="Yes" Or (ALD.EMPLOYMENT)="Y")
  AND
  ((DateDiff("m",IIf(IsDate([EMPLOYMENT_DATE]),DateValue([EMPLOYMENT_DATE]),Date()),Date()))>6)
  AND
  ((IsDate([ALD].[EMPLOYMENT_DATE]))=True))
ORDER BY
  ALD.POOLNUMBER;

/gustav
0
 

Author Comment

by:rfreud
ID: 38329469
To arcee123 - in the messy world of spreadsheet users, this field could contain almost any nonsense that they put in there.  Most commonly they put in "NAV" or "NAP".

The solution provided by gustav worked!  I can't tell you how great this is to have this help.  

If I am interpreting this correctly, the key was to have both the "IsDate" be True AND to have the IsDate tested in the the DateDiff expression using the IIF statement so that the DateDiff function only tries to evaluate actual dates.

It also appears that the "DateValueOfEmploymentDate" is superfluous and could be removed?

Thanks again,
Ron
0
 

Author Closing Comment

by:rfreud
ID: 38329476
Both contributors to this solution helped me a lot, and I really appreciate how quick they were to address this issue and to stick with it when the first possibilities didn't pan out.  Great help - thanks!
0
 
LVL 9

Expert Comment

by:Evan Cutler
ID: 38329482
Gustav got to it before I did...
i was just constructing it.

Good stuff Gustav.
He gets credit.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 38329607
Thanks, you are welcome!

/gustav
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Quickbooks hosting can do wonders to your enterprise but considering the points elaborated in the article which will help you to better analyze the outcomes. So scan your business, its needs and then move to the new world of limitless benefits.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question