Solved

Convert a null to zero...Nz function

Posted on 1997-11-18
10
1,658 Views
Last Modified: 2010-05-18
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...
0
Comment
Question by:sclaverie
10 Comments
 
LVL 1

Expert Comment

by:aprasrl
ID: 1441406
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
 
LVL 1

Author Comment

by:sclaverie
ID: 1441407
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
 
LVL 7

Expert Comment

by:tward
ID: 1441408
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
 
LVL 7

Expert Comment

by:tward
ID: 1441409
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
 
LVL 1

Author Comment

by:sclaverie
ID: 1441410
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Expert Comment

by:tward
ID: 1441411
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
 
LVL 1

Author Comment

by:sclaverie
ID: 1441412
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
 

Accepted Solution

by:
IWinner earned 220 total points
ID: 1441413
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
 
LVL 7

Expert Comment

by:tward
ID: 1441414
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
 
LVL 1

Author Comment

by:sclaverie
ID: 1441415
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now