Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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.
0
mgfranz
Asked:
mgfranz
  • 17
  • 8
  • 5
  • +1
1 Solution
 
xSinbadCommented:
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
 
xSinbadCommented:
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
 
mgfranzAuthor 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

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

eeevans
0
 
eeevansCommented:
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
 
eeevansCommented:
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
 
mgfranzAuthor 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
 
schworakCommented:
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
 
mgfranzAuthor 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
 
mgfranzAuthor 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
 
eeevansCommented:
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
 
mgfranzAuthor Commented:
Oh... thanks evan... that makes me feel younger... ;-)
0
 
mgfranzAuthor Commented:
Great job... thanks.

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

Evan, I'll post another Q for your effort.
0
 
mgfranzAuthor 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
 
eeevansCommented:
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
 
mgfranzAuthor 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
 
schworakCommented:
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
 
mgfranzAuthor 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
 
schworakCommented:
OH! I see what you are after...


If sngGalIceCream=int(sngGalIceCream) then
   txtGalIceCream.Text = sngGalIceCream
else
   txtGalIceCream.Text = Format(sngGalIceCream, "#.##")
end if
0
 
mgfranzAuthor 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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 17
  • 8
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now