Results 1 to 9 of 9
  1. #1
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37

    Data Type Mismatch in Criteria expression in MSQuery but not Access

    Hi,

    I have a query that seems to work fine in MS Access 2007-10 but when I use MS Query to bring it into Excel 2010, I get the error "Data type Mismatch in Criteria Expression".

    Can anyone suggest why as it's causing me all sorts of grief.

    Many thanks in advance.

  2. #2
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    Are you using an MS query in Excel against a worksheet and some of the cells contain no data? My first guess is that it isn't dealing with Nulls (cells with no values). Another guess would be that in Access, all the data types for the query are correctly defined, so it works there. However, on the Excel side you're trying to do something with or against one data type when it is something else (e.g. you're trying to perform an aggregate function on data that looks numeric but is in fact text). Not much info in your post, so guesses are all I have.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37
    Hi Micron,


    I'm not sure what you mean by "against a worksheet".


    I'm bringing in a simple query which only gets data from one table in the access database.


    It does round some fields to 2 decimal places though.


    I liked your idea of nulls possibly being the problem so added the nz() function within my rounding formulas. The query still works fine in Access, but when I try to add it into a blank excel sheet, I get the message "undefined function nz in expression".


    Any other thoughts?


    Thanks for your help.

  4. #4
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    That is a new error. I believe the Nz function is specific to Access. You'd have to write a custom function or try using an If expression.
    As for the old error, I would copy the workbook, open the MS query (I presume you saved it) and remove one field at a time from the query and run it. When the error goes away, you should have isolated the problem field. As previously mentioned, the original error means you are trying to handle a type of data that doesn't conform. That could be nulls in the source data, text you are trying to sum (or other aggregate functions on data types that can't be done), fields with objects in them, or here's a far out one - an Access field is multi value. I have no idea how Excel could handle that.

  5. #5
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37
    Appreciate the ongoing support Micron.

    I only used the NZ function within Access, not Excel.

    Still, I tried replacing the NZ function with iif(isnull(...

    Now I no longer have the NZ error, but still have the data mismatch error.

    I'll try your suggested process of elimination.'

    Thank you

  6. #6
    anasttin is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    37
    Hi Micron.

    Thanks to you, I found the problem!!

    I had a column like this:
    Last_Saved_By: Replace(Replace(Replace([Column R],"No",""),"Zero",""),"Yes","")

    That Excel could not handle for some reason.

    I replaced it with this:
    Last_Saved_By: Replace(Replace(Replace(IIf(IsNull([Column R]),"",[Column R]),"No",""),"Zero",""),"Yes","")

    To handle null values and it now works perfectly.

    Thank you again and have a great day.

  7. #7
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    Looks to me that the 'some reason' was due to nulls. The first expression tries to coerce 3 values to an empty string, but as previously stated, you can't perform functions on null values. Your solution nicely handles null. Glad I was able to help.

  8. #8
    Join Date
    Apr 2017
    Posts
    1,792
    So you did copy the sql-string from Access to Excel, and queried from there?

    Had you simply created a saved query in Access, and in Excel created a query SELECT * FROM YourSavedQuery, you'd avoided this problem at all.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I learned the hard way Excel will not recognize Nz() function. It would not link to query nor even allow VBA to open recordset pulling from query using Nz(). It would not work with query even if it did not use Nz() but any intermediate query objects did.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Data type mismatch in criteria expression.
    By KERRYDEE in forum Access
    Replies: 2
    Last Post: 05-06-2016, 11:56 AM
  2. Data type mismatch in criteria expression.
    By pipoconanan45 in forum Reports
    Replies: 1
    Last Post: 09-09-2015, 06:26 AM
  3. Replies: 4
    Last Post: 05-17-2013, 04:00 PM
  4. Data Type mismatch in criteria expression
    By elb1999 in forum Queries
    Replies: 2
    Last Post: 01-20-2012, 02:38 PM
  5. data type mismatch in criteria expression
    By broecher in forum Access
    Replies: 4
    Last Post: 09-10-2010, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums