Access formula to sum up column values including null

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
Asatoma SadgamayaAnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:
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.
Asatoma SadgamayaAnalystAuthor 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
Fabrice LambertConsultingCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Dale FyeOwner, Developing Solutions LLCCommented:
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]
Gustav BrockCIOCommented:
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 LambertConsultingCommented:
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
Asatoma SadgamayaAnalystAuthor Commented:
Thanks Fabrice, your code worked for me. Also it allows me to format for 2 decimal places.

Thank you all for your contribution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.