Mark Franz
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.
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.
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
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
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.
if sngSundaeEst = "" then
sngSundaeEst = 0
end if
if sngShakeEst = "" then
sngShakeEst = 0
end if
But I need the fields to cls on the submit.
ASKER
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
Private Sub cmdNew_Click()
txtSundaes = ""
txtShakes = ""
txtGalIceCream.Text = ""
txtLbsNuts.Text = ""
txtQtsChocolate.Text = ""
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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?
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
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
ASKER
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
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
ASKER
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?
ASKER
Nope. But I will... ;-)
[I have got to get VBScript out of my head...]
[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
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
ASKER
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)
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)
ASKER
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?]
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
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
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
Cheers,
eeevans
ASKER
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?
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.
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.
ASKER
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... ;-)
It's the little things like that that make my brain wish I didn't belong to a Frat 20 years ago... ;-)
ASKER
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. ?]
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.
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.
ASKER
Oh... thanks evan... that makes me feel younger... ;-)
ASKER
Great job... thanks.
As a "right brain" guy, I'll get it eventually...
Evan, I'll post another Q for your effort.
As a "right brain" guy, I'll get it eventually...
Evan, I'll post another Q for your effort.
ASKER
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 ;-)
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
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
ASKER
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?
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)
# 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)
ASKER
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.
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(sngGalI ceCream) then
txtGalIceCream.Text = sngGalIceCream
else
txtGalIceCream.Text = Format(sngGalIceCream, "#.##")
end if
If sngGalIceCream=int(sngGalI
txtGalIceCream.Text = sngGalIceCream
else
txtGalIceCream.Text = Format(sngGalIceCream, "#.##")
end if
ASKER
Yeah, that works. Thanks again!
sngSundaeEst = txtSundaes
sngShakeEst = txtShakes
if sngSundaeEst = "" then
sngSundaeEst = 0
end if
if sngShakeEst = "" then
sngShakeEst = 0
end if
sngTotalEst = (intOunceIceCreamSundae * sngSundaeEst) + (intOunceIceCreamShake * sngShakeEst)