Solved

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

Posted on 2008-06-16
4
237 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

912 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

24 Experts available now in Live!

Get 1:1 Help Now