Link to home
Start Free TrialLog in
Avatar of Buck Beasom
Buck BeasomFlag for United States of America

asked on

VB2005 version of Access "NZ" function

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.
Avatar of kraiven
kraiven

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.
ASKER CERTIFIED SOLUTION
Avatar of Paul_Harris_Fusion
Paul_Harris_Fusion
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Buck Beasom

ASKER

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.


Outstanding idea!

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

Thanks!
Looks to me like you are missing 23 closing brackets