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
Solved

Convert a null to zero...Nz function

Posted on 1997-11-18
10
1,679 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

860 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