Solved

I keep getting a "data type mismatch in criteria expression" error

Posted on 2008-06-16
4
236 Views
Last Modified: 2013-11-27
I have a query in Access that uses a function to get the month and year from a date field. I pasted the query below. I checked the data type in the table it is querying, and the field is definitely Date/Time

The VBA function is simply;

Public Function GetQNMonthandYear(TheDate As Date)

GetQNMonthandYear = Year([TheDate]) & "-" & Month([TheDate])

End Function
SELECT Format(GetQNMonthandYear([Created On]),"yyyy-mm") AS MonthandYear, Sum(KO_QN_Data.[DefectQty (ext)]) AS Total_QNs

FROM KO_QN_Data

WHERE GetQNMonthandYear([Created on])>='12/30/2005' And (KO_QN_Data.[Name 1]="KO-Borden" OR KO_QN_Data.[Name 1]="flexcel-Borden" OR KO_QN_Data.[Name 1]="flexcel-Salem" OR KO_QN_Data.[Name 1]="KO-Salem" OR (KO_QN_Data.[Name 1]="KO-Jasper 15th Street" OR KO_QN_Data.[Name 1]="flexcel-Jasper 15th Street") And GetFifteenthPlant([Product hierarchy])="Wood Plant") AND GetBucket([Code group])="Product"

GROUP BY Format(GetQNMonthandYear([Created On]),"yyyy-mm")

ORDER BY Format(GetQNMonthandYear([Created On]),"yyyy-mm");

Open in new window

0
Comment
Question by:Rex85
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 21793304
Hello Rex85,

You don't need to apply the format to the returned value as it is already formatted!

   SELECT GetQNMonthandYear([Created On]) AS MonthandYear, Sum(KO_QN_Data.[DefectQty (ext)]) AS Total_QNs
   FROM KO_QN_Data
   WHERE GetQNMonthandYear([Created on])>='2005-12' And (KO_QN_Data.[Name 1]="KO-Borden" OR KO_QN_Data.[Name 1]="flexcel-Borden" OR KO_QN_Data.[Name 1]="flexcel-Salem" OR KO_QN_Data.[Name 1]="KO-Salem" OR (KO_QN_Data.[Name 1]="KO-Jasper 15th Street" OR KO_QN_Data.[Name 1]="flexcel-Jasper 15th Street") And GetFifteenthPlant([Product hierarchy])="Wood Plant") AND GetBucket([Code group])="Product"
   GROUP BY GetQNMonthandYear([Created On])
   ORDER BY GetQNMonthandYear([Created On]);

Regards,

TimCottee
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 21793354
you don't have to use a UDF function to get year month

just use format

SELECT Format([Created On],"yyyy-mm") AS MonthandYear, Sum(KO_QN_Data.[DefectQty (ext)]) AS Total_QNs
FROM KO_QN_Data
WHERE Format([Created On],"yyyy-mm")>='2005-12' And (KO_QN_Data.[Name 1]="KO-Borden" OR KO_QN_Data.[Name 1]="flexcel-Borden" OR KO_QN_Data.[Name 1]="flexcel-Salem" OR KO_QN_Data.[Name 1]="KO-Salem" OR (KO_QN_Data.[Name 1]="KO-Jasper 15th Street" OR KO_QN_Data.[Name 1]="flexcel-Jasper 15th Street") And GetFifteenthPlant([Product hierarchy])="Wood Plant") AND GetBucket([Code group])="Product"
GROUP BY Format([Created On],"yyyy-mm")
ORDER BY Format([Created On],"yyyy-mm");


0
 

Author Closing Comment

by:Rex85
ID: 31467580
Thanks very much. That did it.
0
 

Author Comment

by:Rex85
ID: 21793431
Tim:

Thanks for the help. I tried your solution, but I got the same error message. It must have been in the function. Eliminating it like capricorn1 suggested solved it.

Rex
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now