Solved

When query returns null, I need a zero value as results are used in another query

Posted on 2004-10-26
228 Views
Last Modified: 2010-04-17
I have looked over the answers to what seems to be this question, but do not get them to work.  What I have is a query that if there are no rows to count, does not give me any return value, ie., Null.  However, the results of this query are used in another query and I must have a zero value-otherwise the second query gives me all nulls for the other fields in it despite the fact that their base queries do return values.

SELECT IIf(IsNull(Count([tblDocumentsReturned].[Account])),0,(Count([tblDocumentsReturned].[Account]))) AS MutualFund
FROM tblDocumentsReturned
WHERE (((tblDocumentsReturned.DateReceived)<=[FORMS]![frmMatrixDialog]![txtEnd]) AND ((tblDocumentsReturned.DocStatus)="Mutual Fund"))
GROUP BY tblDocumentsReturned.Product
HAVING (((tblDocumentsReturned.Product)=[FORMS]![frmMatrixDialog]![lstProduct]));

Thanks
0
Question by:ssmith94015
    7 Comments
     
    LVL 2

    Expert Comment

    by:EagleEye1975
    There should be a few ways around this, but we really need to know what development environment you're using.

    There should be a way to get the number of rows returned by the query... that's the first type of function I'd look for.

    If there is no such function, you can do an error trap, saying "If (whatever) = null, NumRows = 0"

    That way, your NULL becomes a zero, and that later function doesn't work with a bunch of NULL values.
    0
     

    Expert Comment

    by:BillJennings
    I'm guessing this is MS-Access.

    It looks to me like this query returns a row for each record from tblDocumentsReturned that meets the criteria, and each of these rows contains a single field (called MutualFund) containing the total number of rows in tblDocmuentsReturned.

    Now,  I'm not sure where you want the zero to be returned.  If there are no rows in tblDocumentsReturned that meet the criteria, it will not return any rows.  Do you want it to always return at least one row?  If so, where would the zero go?

    Sincerely
    Bill Jennings
    0
     
    LVL 2

    Expert Comment

    by:EagleEye1975
    Are you going to give more information or accept an answer?
    0
     

    Author Comment

    by:ssmith94015
    Sorry, I have been out of the office, but am glad for the responses.  What I have is another query based on the results of multiple count queries that combines all these results to be used in a final report.  
    0
     

    Author Comment

    by:ssmith94015
    I am using Access mdb database
    0
     
    LVL 2

    Accepted Solution

    by:
    Yeah, it's pretty simple then...

    All of those count queries need to say "If count = null then count = 0"... so that when they're added together you're not trying to add null, you're adding zero instead.

    Add a layer of abstraction in... don't use the count query results themselves in the adding up of the total.  Do the count queries, and have each result be evaluated and put a final result in to a variable (this would all be done in a module)... then evaluate the variables... if null, then make them equal zero instead... add the result variables, and you have your total without nulls. :)
    0
     

    Expert Comment

    by:BillJennings
    BTW

    "if [count] == null then [count] = 0" can be done inside the query with:

    cnt: nz([count])

    then use [cnt] wherever you were going to use [count].

    Sincerely

    Bill Jennings
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 118
    Problem to adjust sheet 1 71
    sum28 challenge 31 74
    Sql how to exclude from a table 3 51
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now