Link to home
Start Free TrialLog in
Avatar of Mark Franz
Mark FranzFlag for United States of America

asked on

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

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)
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
Avatar of Mark Franz

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of schworak
schworak

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
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
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?
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
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
eeevans, good point... .NET is down the road for me, but something to remember...
Are you issuing a ROUND command some place that I didn't notice?
Nope.  But I will... ;-)

[I have got to get VBScript out of my head...]
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
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)
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?]
the latter would have to pass the value twice and wouldn't save all that much typing.

eeevans
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
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
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?
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.
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... ;-)
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. ?]
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.
Oh... thanks evan... that makes me feel younger... ;-)
Great job... thanks.

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

Evan, I'll post another Q for your effort.
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  ;-)
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
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?
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)
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.
OH! I see what you are after...


If sngGalIceCream=int(sngGalIceCream) then
   txtGalIceCream.Text = sngGalIceCream
else
   txtGalIceCream.Text = Format(sngGalIceCream, "#.##")
end if
Yeah, that works.  Thanks again!