Results 1 to 13 of 13
  1. #1
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142

    Query Field Appearing Empty When Using Criterea

    I have two similar queries, identical SELECT clauses, the only difference is that one has a WHERE clause while the other does not.

    There is one record that both queries correctly retrieve, HOWEVER one of the fields in the query with the WHERE clause is mysteriously blank???



    Here is a screenshot of the returned data and their SQL code. Let me know if you would like me to upload the database.
    (This is Access 2016, latest updates as of today didn't fix the problem.)

    Click image for larger version. 

Name:	1.png 
Views:	7 
Size:	39.4 KB 
ID:	29786

    FYI: The query in question is based off of a UNION query. There are two types of "billings", pay applications and invoices, each have their own table, their own reports, their own problems... Hence the null PayAppID OR InvoiceID depending on which table the data came from. Note that the record in question comes from the invoice table and InvoiceID SHOULD NOT be null.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    try

    try using either ISNULL or NOT ISNULL in your criteria rather than using > 0

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    That didn't work. The receivable field shouldn't ever return a null anyway...

    In case it's relevant: From the previous UNION query that the query in question selects from:
    Receivable = InvoiceAmount - Payments
    where
    InvoiceAmount = IIF( [some field] is null, 0, [some field] )
    Payments = IIF( [some other field] is null, 0, [some other field] )

    Code:
    SELECT qryPayAppAmounts.PayAppID, null AS InvoiceID, qryPayAppAmounts.InvoiceNumber, qryPayAppAmounts.MonthWorked,
    IIF(qryPayAppAmounts.InvoiceAmount is null, 0, qryPayAppAmounts.InvoiceAmount) AS InvoiceAmount,
    IIF(qryPayAppPayments.Payments is null, 0, qryPayAppPayments.Payments) AS Payments,
    qryPayAppAmounts.InvoiceAmount - Payments AS Receivable,
    qryPayAppAmounts.IsPrivate, qryPayAppAmounts.Tier
    FROM qryPayAppAmounts LEFT JOIN qryPayAppPayments ON qryPayAppAmounts.PayAppID = qryPayAppPayments.PayAppID
    
    UNION ALL
    SELECT null AS PayAppID, qryInvoiceAmounts.InvoiceID, qryInvoiceAmounts.InvoiceNumber, qryInvoiceAmounts.MonthWorked,
    IIF(qryInvoiceAmounts.InvoiceAmount is null, 0, qryInvoiceAmounts.InvoiceAmount) AS InvoiceAmount,
    IIF(qryInvoicePayments.Payments is null, 0, qryInvoicePayments.Payments) AS Payments,
    InvoiceAmount - Payments AS Receivable,
    qryInvoiceAmounts.IsPrivate, qryInvoiceAmounts.Tier
    FROM qryInvoicePayments RIGHT JOIN qryInvoiceAmounts ON qryInvoicePayments.InvoiceID = qryInvoiceAmounts.InvoiceID;

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I was talking more about your invoiceID field, where you actually are applying the criteria.

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'm applying the criteria to the Receivable field. I want to return unpaid invoices.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The criteria is applied to Receivable field, not InvoiceID.

    This is bizarre. Think I will need the db to analyze issue.
    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.

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Sorry I took this:

    Note that the record in question comes from the invoice table and InvoiceID SHOULD NOT be null.
    to mean you wanted only non null invoiceID records.

    my guess is that you have a record appearing multiple times in the union query (one for both receivable and payable), but as june said, probably need an example of this database to find out why.

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    See attached. The problem query is Query 1 under Unassigned Objects category. It pulls data from all the queries under the Lien Deadlines category.
    Attached Files Attached Files

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I think there is a non printable character gumming up the works. I am not sure how that's getting into your query because the data looks clean right up until the point you generate query1 but attached is the screenshot I get when I run the query.Click image for larger version. 

Name:	HexProblem.png 
Views:	7 
Size:	75.4 KB 
ID:	29788

    I can get rid of the non printable characters using the TRIM function

    Try using TRIM(INVOICEID) instead of just INVOICEID. Your behavior is different than mine (you don't see anything, I see a hex character) but it should work with the TRIM function.

  10. #10
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Interesting. So then maybe the db engine is having trouble figuring out the datatype of the field? I went back to the UNION query and changed it to return 0 instead of null for PayAppID and InvoiceID respectively (when appropriate). Now Query 1 works as I've told it to. But this feels like a hack, I hesitate to call this solved... It returns IDs that in fact don't exist in the related tables.

    Click image for larger version. 

Name:	2.png 
Views:	7 
Size:	7.7 KB 
ID:	29789

    I tried using TRIM as you suggested, it makes the InvoiceID appear but it's returned as a string instead of a number. CLng(Trim(... fixes that but then we get error's on the null fields. I suppose we could do IIF( InvoiceID IS Null, Null, CLng(Trim(InvoiceID))) ... this is getting hairy. Would I need to do this on the PayAppID field too?

    Code:
    IIF(qryPayAppsAndInvoices.InvoiceID IS NULL, NULL, CLng(Trim(qryPayAppsAndInvoices.InvoiceID))) AS InvoiceID

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The queries all run fine for me, no weird characters.

    Remove ALL from the UNION ALL and there is still a record for InvoiceNumber 105 but there is no InvoiceID and no weird character. So now to determine where this record comes from.

    Never had issues with UNION queries.
    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.

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I wouldn't use cint, I'd use clng if you intend to do anything with the ID field. You also do not need to convert it to a zero. For instance you can just do InvID: clng(trim(InvoiceID)). I'll look at it tonight but I can't answer about PAYAPPID, I suspect it will but I am not basing that on anything other than the source is a union query which sometimes behave weirdly.

  13. #13
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    This bothers me a little bit. I'm thinking I'm going to take a different approach entirely in order to avoid the unions all together...

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

Similar Threads

  1. Replies: 5
    Last Post: 11-17-2016, 12:37 PM
  2. Query syntax to fill a field if empty
    By louise in forum Queries
    Replies: 4
    Last Post: 07-21-2015, 07:26 AM
  3. Replies: 8
    Last Post: 05-16-2012, 09:30 AM
  4. Query Criterea
    By 0000 in forum Queries
    Replies: 2
    Last Post: 05-06-2011, 09:50 PM
  5. Replies: 8
    Last Post: 02-19-2011, 05:48 PM

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