Solved

Convert a null to zero...Nz function

Posted on 1997-11-18
10
1,691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 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