Solved

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

Posted on 2008-06-16
4
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

734 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