Solved

VB2005 version of Access "NZ" function

Posted on 2010-11-17
6
302 Views
Last Modified: 2013-11-26
I have a form that loads existing revenue records into text boxes. It tracks 24 months of revenue, so there are 24 boxes (all of this year and next year by month.) Virtually all of the records have some months with zero revenue.

Since the numeric revenue values are displayed as text in the boxes, calculations involving the text values must first convert the text to a number (I'm using Cdbl). It works for the non-zero values but throws an error for the zero values, which come out as "".

I can probably deal with this problem either by creating a parallel double variable array that stores the values, or by separately testing each text box BEFORE converting it - and skipping the zero values - but both options seem pretty clunky.

In Access, this problem was easily solved by using the "NZ" function to convert all Nulls to 0. Is there something similar in VB2005? Or is there another way to get the Cdbl(txtJanRevenue.Text) function to throw a zero when the txtJanRevenue.Text is "$0.00" = which apparently gets read as the null string?

Thanks.
0
Comment
Question by:Buck_Beasom
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:kraiven
ID: 34154896
Might be better to use Double.TryParse(<string number>, out converted value);
This defaults non-convertible strings to 0.0.

It's in C# I'm afraid but the same method is available to VB.Net, although I'm not sure of the exact syntax around out parameters.
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 500 total points
ID: 34154898
You could easily add an implementation of NZ to your solution I would have thought????

i.e.  in a module

Public Function NZ (inputValue as object) as double
  if IsNumeric(inputValue) then
      return CDbl(inputValue)
  else
      return 0
  end if
End Function
0
 
LVL 3

Expert Comment

by:kraiven
ID: 34154940
Actually pretty easy in vb .net:

dim x as double
dim num as string

num = ""

System.Double.TryParse(num, x)

Open in new window


x contains your type double converted value.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:Buck_Beasom
ID: 34155032
PHF

Great idea. Only one more question:

This line of code:
dblRevenueTotal = CDbl(Nz((Me.txtJanNewCY.Text)) + CDbl(Nz((Me.txtFebNewCY.Text)) & _
            +CDbl(Nz((Me.txtMarNewCY.Text)) + CDbl(Nz((Me.txtAprNewCY.Text)) + CDbl(Nz((Me.txtMayNewCY.Text)) & _
            +CDbl(Nz((Me.txtJunNewCY.Text)) + CDbl(Nz((Me.txtJulNewCY.Text)) + CDbl(Nz((Me.txtAugNewCY.Text)) & _
            +CDbl(Nz((Me.txtSepNewCY.Text)) + CDbl(Nz((Me.txtOctNewCY.Text)) + CDbl(Nz((Me.txtNovNewCY.Text)) & _
            +CDbl(Nz((Me.txtDecNewCY.Text)) + CDbl(Nz((Me.txtJanNewNY.Text)) + CDbl(Nz((Me.txtFebNewNY.Text)) & _
            +CDbl(Nz((Me.txtMarNewNY.Text)) + CDbl(Nz((Me.txtAprNewNY.Text)) + CDbl(Nz((Me.txtMayNewNY.Text)) & _
            +CDbl(Nz((Me.txtJunNewNY.Text)) + CDbl(Nz((Me.txtJulNewNY.Text)) + CDbl(Nz((Me.txtAugNewNY.Text)) & _
            +CDbl(Nz((Me.txtSepNewNY.Text)) + CDbl(Nz((Me.txtOctNewNY.Text)) + CDbl(Nz((Me.txtNovNewNY.Text)) & _
            +CDbl(Nz((Me.txtDecNewCY.Text))

Has the final closing paren underlined in blue. I've tried adding another one and removing it, but regardless I get an error.


0
 

Author Closing Comment

by:Buck_Beasom
ID: 34155051
Outstanding idea!

I figured out the paren problem. Need 3 closing parens after every conversion.

Thanks!
0
 
LVL 3

Expert Comment

by:kraiven
ID: 34155076
Looks to me like you are missing 23 closing brackets
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

828 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