Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

623 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