473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can i have zero values in 2 fields when the criteria in a third field is false

Hello, am having a table called TRANSACTIONS, this table contains 3 fields : TRANSACTION DATE, DRAMOUNT and CRAMOUNT.

I create a Query that ask for a starting transaction date and then shows the totals for the DRAMOUNT and CRAMOUNT for the previous transactions dates.

If i have transactions before the starting date everything is ok but if i don't, i get no results in my query while i want to get zero values.

can someone please help
Feb 12 '12 #1
18 1630
Narender Sagar
189 100+
You can simple use 'If' formula in the query. something like this :
Expand|Select|Wrap|Line Numbers
  1. IIf([DRAMOUNT]>0,[DRAMOUNT],0)
. Similarly for CRAMOUNT. This will serve your purpose.
Feb 12 '12 #2
I am already use the similar Nz function as you can see below

Expand|Select|Wrap|Line Numbers
  1. SELECT   TBLBANKSTRANSACTIONS.[BANK CODE]
  2.        , Sum(Nz([DRAMOUNT],0)) AS BALANCEDR
  3.        , Sum(Nz([CRAMOUNT],0)) AS BALANCECR
  4. FROM     TBLBANKSTRANSACTIONS
  5. WHERE (((TBLBANKSTRANSACTIONS.[TRANSACTION DATE])<[forms]![Report Date Range]![Beginning Trans Date]))
  6. GROUP BY TBLBANKSTRANSACTIONS.[BANK CODE];
I try your advise but i get an error message
any more ideas
Feb 12 '12 #3
NeoPa
32,556 Expert Mod 16PB
The Sum() function already handles null values, but I believe still returns Null if there are no records processed. Thus, the Nz() call should be outside of the Sum() call as :
Expand|Select|Wrap|Line Numbers
  1. Nz(Sum([DRAMOUNT]),0) AS BALANCEDR
Feb 12 '12 #4
The problem is that when i run the query and i don't have any transactions for the days before the transaction date that i enter i get as an answer just the titles of the field names, which is correct but can i get the values zero instead
Feb 13 '12 #5
NeoPa
32,556 Expert Mod 16PB
Is there a good reason why you're asking that instead of trying out the solution already suggested?
Feb 13 '12 #6
I try your suggestion as u can see below but still i have the same problem
Expand|Select|Wrap|Line Numbers
  1. SELECT TBLBANKSTRANSACTIONS.[BANK CODE], Nz(Sum([DRAMOUNT]),0) AS BALANCEDR, Nz(Sum([crAMOUNT]),0) AS BALANCECR
  2. FROM TBLBANKSTRANSACTIONS
  3. WHERE (((TBLBANKSTRANSACTIONS.[TRANSACTION DATE])<[forms]![Report Date Range]![Beginning Trans Date]))
  4. GROUP BY TBLBANKSTRANSACTIONS.[BANK CODE];
Feb 13 '12 #7
NeoPa
32,556 Expert Mod 16PB
Please read [code] Tags Must be Used while I look at what might be going wrong.
Feb 13 '12 #8
NeoPa
32,556 Expert Mod 16PB
Are you simply opening this query, or are you displaying the results somewhere else (like on a form)?

Clearly, if there are no records, there will be nothing shown. That's basic. That you want something to display confuses, and leads me to believe there is still much of the question you haven't explained yet.
Feb 13 '12 #9
This query is used in a report. When i have transactions, my report works OK. For example if i asked to display all the transactions according to a starting date - ending date the report displays firstly a field called BALANCE which is the subtraction of the field BALANCEDR -BALANCECR, this figure is for the days before my starting date and it comes from that query and then rest of the figures for the given days which are from the table TBLBANKSTRANSACTIONS. However if i don't have any figures in the fields BALANCEDR, BALANCECR but still i have transactions for the period that i define in my starting date - ending date my report displays nothing. I believe that if in this case i was having as a value in the field BALANCE the value zero, the report will be ok
Feb 14 '12 #10
NeoPa
32,556 Expert Mod 16PB
Ah. We seem to be getting somewhere near whjat the question should have been.

How is the control [BALANCE] defined? How do you get the result from the SQL query from post #7 to be reflected in there?

How do you refer to this control later? It may be that the reference simply needs to use the Nz() function, but if you want the [BALANCE] value to display zero as well then that wouldn't be a good enough solution.
Feb 14 '12 #11
In my report i add the query BALANCES ACCORDING TO SELECTED DATES that contains the 3 fields,[BALANCEDR], [BALANCECR], [bank code]. I join this query with my table TBLBANKSTRANSACTIONS through the field [bank code]. I then create a calculated field which is called [balance]. I define the field as :
Expand|Select|Wrap|Line Numbers
  1. balance: Nz([TOTAL-BALANCES ACCORDING TO SELECTED DATES].[BALANCEDR]-[BALANCECR],0)
Yes, i want the [BALANCE] value to display zero, if i don't have any transactions in the query BALANCES ACCORDING TO SELECTED DATES so i can run my report
Feb 14 '12 #12
NeoPa
32,556 Expert Mod 16PB
First, please look at [code] Tags Must be Used and make sure you use them in all future posts containing code.

I need to see what is in the RecordSource of your form. If it's a query then I need to see the SQL of that query. Your response was a good one, but I need more information than you replied with to understand exactly what you're dealing with. I may also need the SQL for [BALANCES ACCORDING TO SELECTED DATES], so please include that too.

I suspect the answer will end up including some sort of UNION clause in the SQL somewhere, but I need the extra info to determine exactly where.
Feb 14 '12 #13
The SQL for the query [BALANCES ACCORDING TO SELECTED DATES]is
Expand|Select|Wrap|Line Numbers
  1. SELECT TBLBANKSTRANSACTIONS.[BANK CODE], Sum(Nz([DRAMOUNT],0)) AS BALANCEDR, Sum(Nz([CRAMOUNT],0)) AS BALANCECR
  2. FROM TBLBANKSTRANSACTIONS
  3. WHERE (((TBLBANKSTRANSACTIONS.[TRANSACTION DATE])<[forms]![Report Date Range]![Beginning Trans Date]))
  4. GROUP BY TBLBANKSTRANSACTIONS.[BANK CODE];
  5.  
The RecordSource of my report is called TBLBANKS_TRANSACTIONS and the SQL of that is the following
Expand|Select|Wrap|Line Numbers
  1. SELECT TBLBANKSTRANSACTIONS.ID, TBLBANKSTRANSACTIONS.[TRANSACTION NUMBER], TBLBANKSTRANSACTIONS.[TRANSACTION DATE], TBLBANKSTRANSACTIONS.[AR EGGRAFOU], TBLBANKSTRANSACTIONS.DESCRIPTION, TBLBANKSTRANSACTIONS.DRAMOUNT, TBLBANKSTRANSACTIONS.CRAMOUNT, TBLBANKSTRANSACTIONS.[TYPE OF TRANSACTION], TBLBANKSTRANSACTIONS.[BANK CODE], Nz([TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCEDR-[BALANCECR],0) AS [balance], [TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCEDR, [TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCECR
  2. FROM TBLBANKSTRANSACTIONS INNER JOIN [TOTAL-BALANCES ACCORDING TO SELECTED DATES] ON TBLBANKSTRANSACTIONS.[BANK CODE] = [TOTAL-BALANCES ACCORDING TO SELECTED DATES].[BANK CODE]
  3. WHERE (((TBLBANKSTRANSACTIONS.[TRANSACTION DATE])>=[forms]![Report Date Range]![Beginning Trans Date] And (TBLBANKSTRANSACTIONS.[TRANSACTION DATE])<=[Forms]![Report Date Range]![Ending Trans Date]) AND ((TBLBANKSTRANSACTIONS.[BANK CODE])=[ΠΑΡΑΚΑΛΩ ΔΩΣΕ ΚΩΔΙΚΟ]) AND (([TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCEDR)<[forms]![Report Date Range]![Beginning Trans Date]) AND (([TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCECR)<[forms]![Report Date Range]![Beginning Trans Date]))
  4. ORDER BY TBLBANKSTRANSACTIONS.[TRANSACTION DATE];
  5.  
I hope i used the correct [code] Tags Must be Used, this time. Thank u in advance for your efforts to help me.
Feb 14 '12 #14
NeoPa
32,556 Expert Mod 16PB
I guess your query is probably [TOTAL-BALANCES ACCORDING TO SELECTED DATES] rather than [BALANCES ACCORDING TO SELECTED DATES] as you stated in your post. If not then let me know as I'm going to assume it is (otherwise the query [BALANCES ACCORDING TO SELECTED DATES] is never used and nothing makes sense).

First of all, an INNER JOIN, such as you have, will ensure nothing is included for [BANK CODE]s where no records precede the [Beginning Trans Date] control. We need to change this to a LEFT JOIN.

Next, we need to handle the situation where there are no records and ensure that zeroes are returned anyway. This is not done in [TOTAL-BALANCES ACCORDING TO SELECTED DATES], but rather in [TBLBANKS_TRANSACTIONS]. Each reference to [TOTAL-BALANCES ACCORDING TO SELECTED DATES] needs to use Nz() around it here. Not in the source.

So, we have (I've added some other changes, mostly to the layout, just to make everything more readable) :
Expand|Select|Wrap|Line Numbers
  1. SELECT   tBT.ID
  2.        , tBT.[TRANSACTION NUMBER]
  3.        , tBT.[TRANSACTION DATE]
  4.        , tBT.[AR EGGRAFOU]
  5.        , tBT.DESCRIPTION
  6.        , tBT.DRAMOUNT
  7.        , tBT.CRAMOUNT
  8.        , tBT.[TYPE OF TRANSACTION]
  9.        , tBT.[BANK CODE]
  10.        , Nz(qBals.BALANCEDR-qBals.BALANCECR,0) AS [balance]
  11.        , Nz(qBals.BALANCEDR,0) AS [BalanceDR]
  12.        , Nz(qBals.BALANCECR,0) AS [BalanceCR]
  13. FROM     [TBLBANKSTRANSACTIONS] AS [tBT]
  14.          INNER JOIN
  15.          [TOTAL-BALANCES ACCORDING TO SELECTED DATES] AS [qBals]
  16.   ON     tBT.[BANK CODE] = qBals.[BANK CODE]
  17. WHERE    ((tBT.[TRANSACTION DATE] Between [Forms]![Report Date Range]![Beginning Trans Date] And [Forms]![Report Date Range]![Ending Trans Date])
  18.   AND    (tBT.[BANK CODE]=[???????O ?OS? ?O????])
  19.   AND    (qBals.BALANCEDR<[forms]![Report Date Range]![Beginning Trans Date])
  20.   AND    (qBals.BALANCECR<[forms]![Report Date Range]![Beginning Trans Date]))
  21. ORDER BY tBT.[TRANSACTION DATE]
NB. Lines #18 through #20 look fundamentally wrong to me. I can't be sure, but I would expect that all three lines should go (which would also mean losing the opening parenthesis "(" of line #17 of course) - leaving :
Expand|Select|Wrap|Line Numbers
  1. WHERE    (tBT.[TRANSACTION DATE] Between [Forms]![Report Date Range]![Beginning Trans Date] And [Forms]![Report Date Range]![Ending Trans Date])
  2. ORDER BY tBT.[TRANSACTION DATE]
Feb 15 '12 #15
You was right my code was a bit confusing in those lines so i follow your advice and i changed is as you can see below
Expand|Select|Wrap|Line Numbers
  1. SELECT TBLBANKSTRANSACTIONS.ID, TBLBANKSTRANSACTIONS.[TRANSACTION NUMBER], TBLBANKSTRANSACTIONS.[TRANSACTION DATE], TBLBANKSTRANSACTIONS.[AR EGGRAFOU], TBLBANKSTRANSACTIONS.DESCRIPTION, TBLBANKSTRANSACTIONS.DRAMOUNT, TBLBANKSTRANSACTIONS.CRAMOUNT, TBLBANKSTRANSACTIONS.[TYPE OF TRANSACTION], TBLBANKSTRANSACTIONS.[BANK CODE], Nz([TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCEDR-BALANCECR,0) AS [balance], [TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCEDR, [TOTAL-BALANCES ACCORDING TO SELECTED DATES].BALANCECR
  2. FROM TBLBANKSTRANSACTIONS LEFT JOIN [TOTAL-BALANCES ACCORDING TO SELECTED DATES] ON TBLBANKSTRANSACTIONS.[BANK CODE]=[TOTAL-BALANCES ACCORDING TO SELECTED DATES].[BANK CODE]
  3. WHERE (TBLBANKSTRANSACTIONS.[TRANSACTION DATE] Between Forms![Report Date Range]![Beginning Trans Date] And Forms![Report Date Range]![Ending Trans Date]) And ((TBLBANKSTRANSACTIONS.[BANK CODE])=[Please type the bank code]);
  4.  
I can understand what do you mean by your next tip
"This is not done in [TOTAL-BALANCES ACCORDING TO SELECTED DATES], but rather in [TBLBANKS_TRANSACTIONS]. Each reference to [TOTAL-BALANCES ACCORDING TO SELECTED DATES] needs to use Nz() around it here. Not in the source."

how can i do that?
Feb 19 '12 #16
NeoPa
32,556 Expert Mod 16PB
AnnaKalli:
how can i do that?
If you look at lines #10 through #12 of my code you'll see I've already done it for you ;-)
Feb 20 '12 #17
Thank u so much, it works perfectly now. All my regards from the sunshine Cyprus :-)
Feb 20 '12 #18
NeoPa
32,556 Expert Mod 16PB
You're welcome Anna from Sunny Cyprus :-)
Feb 20 '12 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Salad | last post by:
This may occur to you in the future. You want to get the min or max of a field that you set criteria on...maybe for a combo box. Let's say you wanted to get the minimum of all date fields where...
4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
5
by: Randy Harris | last post by:
How can I report an average of non zero values? If the values are: 5, 0, 6, 0, 4 I would like the result 5 (15 / 3), not 3 (15 / 5) Thanks for any help...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
2
by: cfyrefly | last post by:
I am having a problem figuring out how to do this. Nothing I am doing is correct :-( I have a list of status codes (98 of them) and 3 different percentages for each of these codes...in turn a...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
2
by: exapplerep | last post by:
I've seen how to use VBA code to concatenate two fields into a third by using an expression in the "After Update" property in fields 1 & 2. field3 = field1 + field2 The above code would go...
1
by: DavidB | last post by:
I am working with a database and I would like to be able to populate an array at run time and then use the values that were pushed into the array as criteria for a select query. It seems that this...
1
by: pdesh3 | last post by:
Hi, I am not getting any idea how to implement this: I have a form for which I need to create a top banner kind of the thing on top section of the Form. It should contain the feilds as...
8
by: cppquester | last post by:
I am forced to work with a char* buf; I allocate memory and then construct some data types in buf (several different at different locations of buf kind of struct alike) Sometimes all data...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.