Results 1 to 4 of 4
  1. #1
    baba is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    43

    How to get 0 from a query when no data is found for a criteria

    Hi, I am trying to run the below query. It fetches records when the select criteria is satisfied, however when no records are matched, I would like the query to display 0 .Please help!



    Code:
    select distinct(cars) from tbl1,tbl2 where tbl1.col1 =tbl2.col2 and tbl2.col2 is not null and tbl1.col3 not in (1,2,3,4) 
    and tbl2.col4 = '2013.09'
    Thanks !

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Sorry, regular SELECT queries don't work that way. If no records are retrieved there is an empty recordset, period. Nothing will show.

    However, a UNION might accomplish what you want. Unfortunately there will be a record with 0 value even if the query does return other records.

    select distinct(cars) As Cars from tbl1,tbl2 where tbl1.col1 =tbl2.col2 and tbl2.col2 is not null and tbl1.col3 not in (1,2,3,4) and tbl2.col4 = '2013.09'
    UNION 0 FROM tbl1;

    There is no wizard or designer for UNION, must type in SQL View window.

    Why is this needed?

    Is this query the RecordSource for a form or report?
    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.

  3. #3
    baba is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    43
    Hi,
    I want to get a zero in the query only when no records are found. I think using NZ or IIF in the SQL would work but I dont know how to create the query using IIF or NZ that would return a zero.

    Thanks


    Quote Originally Posted by baba View Post
    Hi, I am trying to run the below query. It fetches records when the select criteria is satisfied, however when no records are matched, I would like the query to display 0 .Please help!

    Code:
    select distinct(cars) from tbl1,tbl2 where tbl1.col1 =tbl2.col2 and tbl2.col2 is not null and tbl1.col3 not in (1,2,3,4) 
    and tbl2.col4 = '2013.09'
    Thanks !

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    No, IIf or Nz will not work because there is no record returned. There is no way to IIf or Nz a query. Those are functions that can be used in a query to calculate fields. If no records meet the filter criteria, no records will return.

    Again, what purpose would this serve?
    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. Error Data macro could not be found
    By RoyLittle0 in forum Access
    Replies: 2
    Last Post: 12-26-2012, 10:50 AM
  2. Method or data member not found
    By papa yaw in forum Programming
    Replies: 5
    Last Post: 12-17-2012, 02:19 PM
  3. Replies: 3
    Last Post: 12-04-2012, 01:09 PM
  4. Replies: 3
    Last Post: 09-12-2012, 02:57 PM
  5. Compile Error: Method or data member not found
    By subtilty in forum Programming
    Replies: 5
    Last Post: 02-09-2012, 07:56 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