Simple txtValue Q.

Ok... I'm very new at using VB, and I have already hit a snag.  Using a simple form, if a txtField is submitted "blank" or "empty", I get an error 13;

Private Sub cmdCalc_Click()

' Set constants for operations
Const intOunceIceCreamSundae = 7
Const intOunceNutsSundae = 1
Const intOunceChocSauceSundae = 2
Const intOunceIceCreamShake = 12
Const intOunceChocShake = 1

' Declare the vars
Dim sngGalIceCream As Single, sngLbsNuts As Single, sngQtsChocolate As Single
Dim sngSundaeEst As Single, sngShakeEst As Single, sngTotalEst As Single

' Start by retrieving the form estimates

sngSundaeEst = txtSundaes
sngShakeEst = txtShakes
sngTotalEst = (intOunceIceCreamSundae * sngSundaeEst) + (intOunceIceCreamShake * sngShakeEst)

' Calculate the results
sngGalIceCream = sngTotalEst / 128

' Now post the results to the txtBox's
txtGalIceCream.Text = sngGalIceCream
txtLbsNuts.Text = sngLbsNuts
txtQtsChocolate.Text = sngQtsChocolate
End Sub

If txtSundaes or txtShakes is submitted populated all is fine, if one of the fields is empty I get the 13 error.

[be nice... I'm very new with this...]

Also, whats the best way to create a "Clear" or "New" command button, I have tried using cmdNew.Refresh and cmdNew.Cls but they dont work.
LVL 18
Who is Participating?

Commented:
Make it simple guys...

Text boxes are exactly that, text. You can not do math of any kind on text. What happens behind the scenes is VB converts the number for you. If you type ABC into the text box or leave it blank you will get the same error. VB doesn't know how to convert it to a number.

If you on the other hand do the conversion for VB you will never get the error.

The VAL function turns any string into a number. If the text is blank or ABC or any NON-NUMBER you will get a value of zero back.

sngSundaeEst = VAL(txtSundaes)
sngShakeEst = VAL(txtShakes)

But take note that if you really want to be sure the user put in a number before going on you should do a little more for them.

For example, if you want to tell the user they entered invalid characters you would need to check that they only put in numbers and no other characters. The fix above will do what you are after but to test first you need a little more. I won't list the more here unless you want an example.

Just remember, any time you want to turn text into a number use the VAL function and don't leave it up to VB to figure out what to do. It will make mistakes at times.
0

Commented:
mgfranz, you really need to have a value like 0 or test for the blank before you try and use the textbox value, i.e.

sngSundaeEst = txtSundaes
sngShakeEst = txtShakes

if sngSundaeEst = "" then
sngSundaeEst = 0
end if

if sngShakeEst = "" then
sngShakeEst = 0
end if

sngTotalEst = (intOunceIceCreamSundae * sngSundaeEst) + (intOunceIceCreamShake * sngShakeEst)
0

Commented:
What do you what the Clear button to do?

If it is just to clear all the fields then just do something like this;

Private Sub Command1_Click()
txtSundaes = ""
txtShakes = ""
End Sub

or a better option in your case would be;

Private Sub Command1_Click()
txtSundaes = 0
txtShakes = 0
End Sub

Cheers
Marcus
0

Author Commented:
I did this exact thing;

if sngSundaeEst = "" then
sngSundaeEst = 0
end if

if sngShakeEst = "" then
sngShakeEst = 0
end if

But I need the fields to cls on the submit.
0

Author Commented:
And surely there has to be a better way than this to Cls;

Private Sub cmdNew_Click()
txtSundaes = ""
txtShakes = ""
txtGalIceCream.Text = ""
txtLbsNuts.Text = ""
txtQtsChocolate.Text = ""
End Sub
0

Commented:
I think what xSinbad meant to code was:

If txtSundaes.text = "" then
sngSundaeEst = 0
Else

End If

If txtShakes.text = "" then
sngShakeEst = 0
End If

In VB6 if you substitute If Len(txtShakes.text) = 0 then it will execute a little faster.

Also if you want the text boxes to clear on submitting just set them equal to ""

txtSundaes.text = ""
txtShakes.text = ""

HTH

eeevans
0

Author Commented:
schworak, using the VAL() worked. (remeber, this is a VERY basic app, no need to build validation and such)

Now... where can I find a reference source that can expand on issues like this?
0

Commented:
oops, if only we could edit our comments until the question is marked as answered.  Any way here is the code.

If Len(txtSundaes.text) = 0 then
sngSundaeEst = 0
Else
sngSundaeEst = txtSundaes.text
End If

If Len(txtShakes.text) = 0 then
sngShakeEst = 0
Else
sngShakeEst = txtShakes.text
End If

You might also want to start getting used to typing out the default property (in this case .text) as VB.Net does not use default properties.  (if you want to consider .Net)

HTH,

eeevans
0

Author Commented:
OK... really quick.

If I enter a value of "55" in the txtSundae field, the resulting data in the txtGalIceCream field is "3.007813"

While the algorithm is correct, I'm confused as to why the SngVar doesn't truncate the Int to 2 decimals?  It should be 3.01
0

Author Commented:
eeevans, good point... .NET is down the road for me, but something to remember...
0

Commented:
Are you issuing a ROUND command some place that I didn't notice?
0

Author Commented:
Nope.  But I will... ;-)

[I have got to get VBScript out of my head...]
0

Commented:
mgfranz,
As schworak said, you are letting VB do all the converting so when you do the division, it keeps all the precision. You have to format it as a string like you want.

txtGallceCream.text = format(sngGalIceCream, "0.##")

Or you can use Round if you will need the single var to be that amount later on.

eeevans
0

Author Commented:
OK... one more Q then I'm done for the night.

Is it more efficient to use a simple Object() call or pass a value to a Sub?

Which is more efficient?

'Now post the results to the txtBox's
txtGalIceCream.Text = Round(sngGalIceCream, 2)
txtLbsNuts.Text = Round(sngLbsNuts, 2)
txtQtsChocolate.Text = Round(sngQtsChocolate, 2)

Or...

Public Sub RoundIt(rndVal)
0

Author Commented:
Sorry...  Let me try again...

Public Sub RoundIt(rndVal)
Round(rndVal, 2)
End Sub

'Now post the results to the txtBox's
txtGalIceCream.Text = RoundIt(sngGalIceCream)
txtLbsNuts.Text = RoundIt(sngLbsNuts)
txtQtsChocolate.Text = RoundIt(sngQtsChocolate)

[you get my drift right?]
0

Commented:
the latter would have to pass the value twice and wouldn't save all that much typing.

eeevans
0

Commented:
I meant latter as in your previous post.  Passing to the Sub and then the sub passing to the Round function would double the calls.

eeevans
0

Commented:
As you are new to VB, have you tried Ctrl-Spacebar after typing the first part of a variable name?  Or if you place the cursor in the middle of a bunch of arguments to a function or Sub and press Ctrl-Shift-Spacebar?

Cheers,

eeevans
0

Author Commented:
Right...

Round worked fine...

Val and Round fixed my problems.

For an extra 50, who has a reference link like www.devguru.com for VB6?
0

Commented:
In this case it makes so little difference you should do what you are more comfortable with.

The tip I like to use is if the number is going to be presented without any fancy formatting use the ROUND function.

But if you want to be sure there are always 2 decimal places or you want to have commas in the output (1,000.00) then you MUST use the FORMAT function.

Lets assume your value is 1000.000001 and you want it rounded to 2 decimal points...

txtGalIceCream.Text = Round(sngGalIceCream, 2)

Will result in 1000 (no decimal points because they are 0)

txtGalIceCream.Text = Format(sngGalIceCream, "#,##0.00")

Will result in 1,000.00

txtGalIceCream.Text = Format(sngGalIceCream, "#0.00")

Will result in 1000.00

Formated output use the FORMAT function, non formated output use ROUND.
0

Author Commented:
Ctrl-Spcbr yes, Ctrl-Shft-Spcbr no...

It's the little things like that that make my brain wish I didn't belong to a Frat 20 years ago... ;-)
0

Author Commented:
Yeah see... in VBScript FormatNumber() allows you to set the number of places.

When I place Format() I get "day of week, format week, ..." etc...

Where did you get the expressions #?

Format(sngGalIceCream, "#,##0.00")

[Or am I really Homer J. ?]
0

Commented:
http://www.mvps.org/vbnet/
http://searchvb.techtarget.com/
http://www.softcircuits.com/sw_vbsrc.htm

Wow, most of the links I have had from the past are broken and the sites are gone.  I guess most are moving on to .Net.
0

Author Commented:
Oh... thanks evan... that makes me feel younger... ;-)
0

Author Commented:
Great job... thanks.

As a "right brain" guy, I'll get it eventually...

Evan, I'll post another Q for your effort.
0

Author Commented:
Hey, real quick... how do I trim the 0. off the returned value of 0.55?

txtQtsChocolate.Text = Round(sngQtsChocolate, 2)

I need it to return .55  ;-)
0

Commented:
Format(sngGallceCream, "#.00")

That way it will only put a number if there is one. The 0.00 says always have at least one digit to the left of the decimal and always 2 to the right.  So 2.1 would be 2.10 and 2 would be 2.00.  If you put #.## 2.1 would be 2.1 and 2 would be 2. and .55 would be .55. As it would in #.00 as well.

Regards,

eeevans
0

Author Commented:
If I use this;

txtGalIceCream.Text = Format(sngGalIceCream, "#.##")

And the resulting number in sngGalIceCream is 4.0, the output of txtGalIceCream is 4.

How can I get around this?
0

Commented:
Use ZERO not # to force the place holder.

# means only show if non zero at the end of the number

txtGalIceCream.Text = Format(sngGalIceCream, "#.##")

0.0 = . (no digits showing)

txtGalIceCream.Text = Format(sngGalIceCream, "0.00")

0.0 = 0.00 (decimal places are held)
0

Author Commented:
See, there in lies the problem, the output might be in any number of scenarios;

4
4.5
.5
12.32
123.4

etc...

Never more than 2 decimals to the right though.
0

Commented:
OH! I see what you are after...

If sngGalIceCream=int(sngGalIceCream) then
txtGalIceCream.Text = sngGalIceCream
else
txtGalIceCream.Text = Format(sngGalIceCream, "#.##")
end if
0

Author Commented:
Yeah, that works.  Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.