Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93

    Query criteria Type Mismatch

    I have a calculated field (Shortage Qty) in my main table [Load Lists]. (Shortage Qty)=(Returned Qty)-(Sent Qty). This works great. It calculates my shortage (what I sent out vs what I got back). I was surprised, considering my sent and returned qtys are both formatted as text.
    Now, I want to have a query show only the negative numbers in (Shortage Qty), to isolate shortages only. Problem is, when I enter the expression "<0", it gives me a type mismatch error. (Shortage Qty) is set to be a calculated field type, with an integer result.




    Is there a way around this, without converting (Returned Qty) & (Sent Qty) to numeric types? I really don't want to do that, as some of my imported data contains text, and changing these fields to numeric throws me a bunch of errors with other parts of my database.

    I would attached a sample, but my database is huge!

    Any help would be appreciated! Thanks.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Try:
    CInt((Shortage Qty)=(Returned Qty)-(Sent Qty))
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Wolfm -

    When you entered "<0" as the criteria, did you use the quotation marks, or are you just using them in this discussion? If you did use them, that is your error - just put <0 as the criteria, without quotes.

    John

  4. #4
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Quote Originally Posted by Bob Fitz View Post
    Try:
    CInt((Shortage Qty)=(Returned Qty)-(Sent Qty))
    Do I use this expression in my query, or in my original table containing the value?
    When I use CInt([Shortageqty]=[ReturnedQty]-[Qty]) in my query, I get the error:
    “Invalid Use of Null”

    When I use CInt([ReturnedQty]-[Qty]) or CInt( [Shortageqty] = [ReturnedQty] - [Qty] ) in my calculated field in my original table, I get the error:
    “The expression CInt([ReturnedQty]-[Qty]) cannot be used in a calculated column”

    Thanks!

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You have to apply the CInt function to each value individually, I think.

    Try it like this: CInt([ReturnedQty]) - CInt([Qty])

    If either [ReturnedQty] or [Qty] can be Null, use this:

    CInt(Nz([ReturnedQty],"0")) - CInt(Nz([Qty],"0"))


    John
    Last edited by John_G; 04-28-2014 at 02:36 PM. Reason: Additional information

  6. #6
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    John-- When I just enter <0, it says "type mismatch". I have tried with and without quotation marks.

  7. #7
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Wolfm -

    Using the Cint / Nz to force the expression to integer type should fix that. If it doesn't, can you post the SQL of the query?

    One other thing you can try is to do the calculation in the query, rather than use the calculated field from the table, with the expression for the query field being:

    CInt(Nz([ReturnedQty],"0")) - CInt(Nz([Qty],"0")) (as before)


    John

  8. #8
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    When I tried this, I get "Type Mismatch"

    "One other thing you can try is to do the calculation in the query, rather than use the calculated field from the table, with the expression for the query field being:

    CInt(Nz([ReturnedQty],"0")) - CInt(Nz([Qty],"0")) (as before)"

  9. #9
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Quote Originally Posted by John_G View Post
    You have to apply the CInt function to each value individually, I think.

    Try it like this: CInt([ReturnedQty]) - CInt([Qty])

    If either [ReturnedQty] or [Qty] can be Null, use this:

    CInt(Nz([ReturnedQty],"0")) - CInt(Nz([Qty],"0"))


    John
    When I try this in the original field in my table, it says I cannot use Cint in a calculated field.
    When I try in the query it says “Invalid Use of Null”

  10. #10
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Sample definitions.accdb
    Quote Originally Posted by John_G View Post
    Wolfm -

    Using the Cint / Nz to force the expression to integer type should fix that. If it doesn't, can you post the SQL of the query?

    One other thing you can try is to do the calculation in the query, rather than use the calculated field from the table, with the expression for the query field being:

    CInt(Nz([ReturnedQty],"0")) - CInt(Nz([Qty],"0")) (as before)


    John
    My data file is attached, less records.

  11. #11
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Can you post a copy in A2003 mdb format.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  12. #12
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Quote Originally Posted by Bob Fitz View Post
    Can you post a copy in A2003 mdb format.
    Acess will not allow me to convert to mdb. It says database requires newer features.
    Anyway, attached is a reduced version with some data, and the relevant table and query. All I really need to do is have the query filter out records that contain zero or positive numbers in the shortage qty field. In other words, show only items with a shortage, shown as a negative number. Thank you!Sample definitions.mdb.accdb

  13. #13
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Quote Originally Posted by wolfm View Post
    Acess will not allow me to convert to mdb. It says database requires newer features.
    Anyway, attached is a reduced version with some data, and the relevant table and query. All I really need to do is have the query filter out records that contain zero or positive numbers in the shortage qty field. In other words, show only items with a shortage, shown as a negative number. Thank you!Sample definitions.mdb.accdb
    The db you attached is not A2003 format
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  14. #14
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    "When I try in the query it says “Invalid Use of Null” "

    That's odd - the expression I used should prevent that error. Is there something else in the query that might be causing the error? Can you post the SQL of the query?

    John

  15. #15
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Will this work?

    Code:
    SELECT [Load Lists].Qty, [Load Lists].Description, [Load Lists].[Job Name], [Load Lists].[Job #], [Load Lists].DateOut, [Load Lists].Trackable, [Load Lists].DateBack, [Load Lists].ReturnedQty, [Load Lists].ShortageqtyFROM [Load Lists]
    WHERE ((([Load Lists].Trackable)=1))
    ORDER BY [Load Lists].Shortageqty;

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2013, 11:37 AM
  2. Replies: 4
    Last Post: 05-17-2013, 04:00 PM
  3. Data type mismatch in criteria expression
    By buienxg in forum Access
    Replies: 2
    Last Post: 11-22-2011, 10:29 AM
  4. Data type mismatch in query criteria
    By TinaCa in forum Queries
    Replies: 2
    Last Post: 09-19-2011, 11:31 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