Access formula to sum up column values including null

Asatoma Sadgamaya
Asatoma Sadgamaya used Ask the Experts™
on
Hi

I am trying to sum up columns and get the result using below formulae in access query. values need to sum up are in text format ("0.00")

Total: Nz(CDbl([Spent]),0)+Nz(CDbl([To Spend]),0)

I am getting #Error

Please have a look

Thank you
A
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Hi,

Is that in a form ? a report ? a query ?
From where your data comes from ?
Please, elaborate.

Note:
Most of the time, you don't need to use the Nz function with Access, it handle Null values without problems.

Author

Commented:
Hi Fabrice,

Its for Access query and the data come from a text file I linked with the database. As you have mentioned, I have taken out nz function out of that formula, still has got error

If one column has value and the other does not, then the out put is #Error.
If both has got value in it, the formula gives correct value.

Thank you
A
Consulting
Distinguished Expert 2017
Commented:
Text files, still a source of troubles ....

The Cdbl function does not support null values, so it should call the Nz function. You did the opposit (Nz calling Cdbl)

Try with:
Cdbl(Nz([Spent, 0))

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
if these are in a column, you might try using a function, something like:

Public Function fnSum(ParamArray ArrayOfValues() As Variant) As Variant

    Dim varSum As Variant
    Dim intLoop As Integer
          
    varSum = 0
    For intLoop = LBound(ArrayOfValues) To UBound(ArrayOfValues)
        If IsNull(ArrayOfValues(intLoop)) Then
            'do nothing
        ElseIf Trim(ArrayOfValues(intLoop) & "") = "" Then
            'do nothing
        ElseIf IsNumeric(ArrayOfValues(intLoop)) = False Then
            'do nothing
        Else
            varSum = varSum + Val(ArrayOfValues(intLoop))
        End If
    Next
    fnSum = varSum
          
End Function

Open in new window


This function accepts an array of values, so you can pass in as many values as you want, and can be used anywhere in your application, not just in a query. It ignores NULLs, empty strings, and non-numeric data.

Then, in your query use something like:

Total: fnSum([Spent], [ToSpend]
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
If the field holds text, so should the replace with text.
But as Nz defaults to "" (empty string), you could reduce the expression using Val:

Total: Val(Nz([Spent]))+Val(Nz([To Spend]))

Open in new window

Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
Also, depend on your needs, it might be wize to import (maybe validate) and convert your text file into your database, so it is easyer to handle.

And should you care about database size inflation, import in a temporary db that you'll delete once you no longer need it (typically when closing the application).
You might pay a loading time, but this will save you lots of troubles afterward, as you'll have manageable data at your disposal

Author

Commented:
Thanks Fabrice, your code worked for me. Also it allows me to format for 2 decimal places.

Thank you all for your contribution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial