Solved

Simple txtValue Q.

Posted on 2002-07-03
32
197 Views
Last Modified: 2010-05-02
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
Comment
Question by:mgfranz
  • 17
  • 8
  • 5
  • +1
32 Comments
 
LVL 6

Expert Comment

by:xSinbad
Comment Utility
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
 
LVL 6

Expert Comment

by:xSinbad
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 3

Accepted Solution

by:
schworak earned 50 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
eeevans, good point... .NET is down the road for me, but something to remember...
0
 
LVL 3

Expert Comment

by:schworak
Comment Utility
Are you issuing a ROUND command some place that I didn't notice?
0
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
Nope.  But I will... ;-)

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

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
the latter would have to pass the value twice and wouldn't save all that much typing.

eeevans
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 3

Expert Comment

by:schworak
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
Oh... thanks evan... that makes me feel younger... ;-)
0
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
Great job... thanks.

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

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

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 1

Expert Comment

by:eeevans
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 3

Expert Comment

by:schworak
Comment Utility
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
 
LVL 18

Author Comment

by:mgfranz
Comment Utility
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
 
LVL 3

Expert Comment

by:schworak
Comment Utility
OH! I see what you are after...


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

Author Comment

by:mgfranz
Comment Utility
Yeah, that works.  Thanks again!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

7 Experts available now in Live!

Get 1:1 Help Now