Solved

VB2005 version of Access "NZ" function

Posted on 2010-11-17
6
296 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 new code templates in NetBeans IDE 8.0 for Windows.

760 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

19 Experts available now in Live!

Get 1:1 Help Now