Convert a null to zero...Nz function

In VB, how can I force a field in a query to give me back a zero???  In Acess, I would say within the query, "select Nz(<fieldName>)...
However, Nz is undefined in VB...
I need to do this within a SQL statement to have the db execute...
i.e. db.Execute strSQLQuery

Any help would be appreciated...
LVL 1
sclaverieAsked:
Who is Participating?
 
IWinnerConnect With a Mentor Commented:
What you are asking...cannot be accomplished on the VB side...
If you were returning these items in some sort of reporting tool, like Access or Crystal Reports...then you could set the output to convert the Nulls to Default Value...(In Crystal Reports, go to the Report Options under the file menu and click on the reporting tab...)  That should work...

The only other thing would be to try and do this in Access and make it executable with ADT...but that's a little too expensive of a solution...

Hope that works for ya...
0
 
aprasrlCommented:
Why don't you set the DefaultValue for the field to zero ?

Or you could use a statement like this

Select (0+FieldName) As TempField ....
0
 
sclaverieAuthor Commented:
Sorry, that didn't work...
I also need to be able to add all these Nulls (which will eventually be zeros) together...
Kind of like a spreadsheet...

Anyway, I can't set the DefaultValue to Zero, because I am in a query...I can only set a default value in a table...and I'm making the table dynamically...

I have tried the (0 + fieldName) as well as Val(fieldName)
Neither of these worked...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
twardCommented:
Use the SQL statement to create a Dynaset:

Dim NewDynaset As Recordset
Dim Total As Long
set NewDynaset = db.OpenRecordset("SELECT FIELDNAME FROM TABLE", dbOpenDynaset)

then you can access the field like this:

Total = val(NewDynaset.fields("FIELDNAME").value & "")

The & "" should take care of a NULL field.
0
 
twardCommented:
Another thing you can also do if you are simply looking for a total:

Dim NewDynaset as Recordset
Dim Total as Long

Set NewDynaset=db.OpenRecordset("SELECT SUM(FIELDNAME) AS TOT_FIELD FROM TABLE", dbOpenDynaset)

Total = NewDynaset.fields("TOT_FIELD").value

This simply makes an alias (TOT_FIELD).
0
 
sclaverieAuthor Commented:
I already tried converting it to a string, the only problem is that a) it didn't work and b) i need to sum the fields...

Sorry...
0
 
twardCommented:
That is what the comment was that I added.  This will give you th e sum:

Dim NewDynaset as Recordset
Dim Total as Long

Set NewDynaset=db.OpenRecordset("SELECT SUM(FIELDNAME) AS TOT_FIELD FROM TABLE", dbOpenDynaset)

Total = NewDynaset.fields("TOT_FIELD").value

This simply makes an alias (TOT_FIELD).
0
 
sclaverieAuthor Commented:
Sorry...
Those two answers didn't work...
I have already tried converting it to a string using the "" and I have already tried the val() function
I also need to be able to add all of these together to get a sum of all values...
Thanx anyway...
0
 
twardCommented:
What I gave you will sum the fields, it is not turning anything into a string...  The second answer I gave (that was a comment at first) will return in a DynaSet the sum of whatever field you tell it to sum....

From your question that is all I understand that you want to do...?

You want to get the sum of a numeric field from a database and put it in a VB variable..?  The second answer I gave is the one that will sum fields for you...

What don't you understand?
0
 
sclaverieAuthor Commented:
I ended up using the Crystal Report method...

The reason what tward was proposing would not work is because I am averaging numbers first...this is where I get the null, then I want the sum of the averages...what tward proposed would only get rid of the null for the sum, not the averages...
Crystal Reports did the job though...thanx.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.