Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107

    Calculation issue in a query


    This is so simple but it isn't working so I'm missing something obvious. I created a query with a deposit field for the month of January. In that same query I have a debit field. I created a second query that sums the deposit and the debit column. I then created a field that took the deposit sum and subtracted the debit sum for a total amount left. But when I run the query the deposit field and debit field are correct but the amount left is empty.

    What am I doing wrong?

    I'll try to post a pic.
    Attached Thumbnails Attached Thumbnails Query 1.jpg  

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    In your calculation field, change the "Total" row value from "Group By" to "Sum" also.

  3. #3
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by JoeM View Post
    In your calculation field, change the "Total" row value from "Group By" to "Sum" also.
    Tried it... the field is still blank.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Are your Transaction and Deposit fields populated with numbers for ALL records?
    Also, get rid of the space in your field name, i.e. use Left_Over.

  5. #5
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by JoeM View Post
    Are your Transaction and Deposit fields populated with numbers for ALL records?
    Also, get rid of the space in your field name, i.e. use Left_Over.
    No. I have 31 records. 2 are deposits and the rest are debits. So 29 or the deposit records are blank and 2 of the debit records are blank.

    I removed all spaces in my calculation. Access added on space back and I changed Left Over to Left_Over.

    The field is still blank.

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Sometimes, Access chokes if you try to do too much in one query.

    However, let's try this. Try changing your calculation to this:
    Left_Over: Nz([Transaction_Deposit],0) - Nz([Transaction_Debit],0)

  7. #7
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by JoeM View Post
    Sometimes, Access chokes if you try to do too much in one query.

    However, let's try this. Try changing your calculation to this:
    Left_Over: Nz([Transaction_Deposit],0) - Nz([Transaction_Debit],0)
    Okay the image I posted was the complete Query. To me that's a pretty simple Query and a pretty simple calculation.

    With that said you formula worked. Why? Also... I'm just learning this stuff so... be gentle...

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You must account for all the records with Nulls in your query. Note that Access doesn't always do things in the order you expect. Sometimes, it tries to do calculations BEFORE applying any criteria that you have. So, even if you are filtering out records with Nulls, they may still mess up your calculation.

    The Nz function handles Null values. It tells you want to return in case of a null. We cannot do Sums with Nulls, so we tell it to return a 0 in its place.
    See this for details on the Nz function: http://www.techonthenet.com/access/f...dvanced/nz.php

    Hope that helps!

  9. #9
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by JoeM View Post
    You must account for all the records with Nulls in your query. Note that Access doesn't always do things in the order you expect. Sometimes, it tries to do calculations BEFORE applying any criteria that you have. So, even if you are filtering out records with Nulls, they may still mess up your calculation.

    The Nz function handles Null values. It tells you want to return in case of a null. We cannot do Sums with Nulls, so we tell it to return a 0 in its place.
    See this for details on the Nz function: http://www.techonthenet.com/access/f...dvanced/nz.php

    Hope that helps!
    It helps a ton Joe thanks.

  10. #10
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Here's a question Joe. Is it possible to make the Left_Over field distinguish between a negative value and a positive value. For example if the debits are more then the deposits the value in the field would be negative. And then if you really wanted to impress me can a negative number be shown in red and a positive number be shown in green?

  11. #11
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It should already return a negative if your total debits are more than your total deposits, as you would be subtracting a bigger number from a smaller one.

    As for coloring the text, you can use Conditional Formatting on Access Forms or Reports (but not Queries).
    See: https://support.office.com/en-sg/art...5-44e43b05e22f
    and http://www.techrepublic.com/blog/mic...s-and-reports/

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by JoeM View Post
    <snip>

    As for coloring the text, you can use Conditional Formatting on Access Forms or Reports (but not Queries).
    </snip>
    Try this in the query.
    In design view of the query, right click in the column "Left_Over".
    Click on properties (bottom of the list)

    In the FORMAT row, enter
    Code:
    $#,##0.00[Green];($#,##0.00)[Red]
    Close the properties dialog window.
    Execute the query.

  13. #13
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Try this in the query.
    In design view of the query, right click in the column "Left_Over".
    Click on properties (bottom of the list)

    In the FORMAT row, enter
    Code:
    $#,##0.00[Green];($#,##0.00)[Red]
    Close the properties dialog window.
    Execute the query.
    Clever. I stand corrected!

  14. #14
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by JoeM View Post
    It should already return a negative if your total debits are more than your total deposits, as you would be subtracting a bigger number from a smaller one.

    As for coloring the text, you can use Conditional Formatting on Access Forms or Reports (but not Queries).
    See: https://support.office.com/en-sg/art...5-44e43b05e22f
    and http://www.techrepublic.com/blog/mic...s-and-reports/
    Okay... Rather than a negative my value comes back () on either side of it.

  15. #15
    Collins is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    107
    Quote Originally Posted by ssanfu View Post
    Try this in the query.
    In design view of the query, right click in the column "Left_Over".
    Click on properties (bottom of the list)

    In the FORMAT row, enter
    Code:
    $#,##0.00[Green];($#,##0.00)[Red]
    Close the properties dialog window.
    Execute the query.
    You guys are the masters of disaster!!!! I love this forum!!! That is so cool...

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

Similar Threads

  1. Replies: 5
    Last Post: 01-08-2015, 02:08 PM
  2. Calculation issue
    By keiath in forum Forms
    Replies: 3
    Last Post: 03-09-2014, 09:27 AM
  3. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 PM
  4. Query Calculation Issue
    By GraemeG in forum Queries
    Replies: 0
    Last Post: 06-15-2011, 07:36 AM
  5. IIF() in a query calculation
    By bbylls in forum Queries
    Replies: 4
    Last Post: 12-03-2009, 04:42 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