Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-16
4
Medium Priority
?
244 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

972 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