Forum archive - what wrong with this code?

http://www.supermediastore.com/



Forum Archive Home -> Programming -> what wrong with this code?



what wrong with this code?

emogal posted 2009 Jan 11 02:54
Can tell me what's wrong with this code? This code doesn't seem to works and it pop up a thing that says INVAILDCASTEXCEPTION WAS UNHANDLED and it highlighted the statement of txtHousingAllowance.Text = txtBasicSalary.Text * 0.2 this. So can tell what the problem with it?

I am really struck with It. U are an expert in vb programming, can u help me to write code for me or u can mofify it if u dun mind..... In the second post i have the pic in but i dun noe how to write the code......
For the housing allowance: 20% of the basic salary
For the travel allowance: 5% of the basic salary
For the family allowance: $500


txtHousingAllowance.Text = txtBasicSalary.Text * 0.2
txtTravelAllowance.Text = txtBasicSalary.Text * 0.05
txtTotalAmount.Text = Val(txtBasicSalary.Text) + Val(txtOverTime.Text) + Val(txtHousingAllowance.Text) + Val(txtTravelAllowance.Text) + Val(txtFamilyAllowance.Text)

Another code is this it also had the same problem.

If txtCPFContribution.Text = 20 Then
txtCPFDeduction.Text = txtBasicSalary.Text * 0.33
ElseIf txtCPFContribution.Text = 18 Then
txtCPFDeduction.Text = txtBasicSalary.Text * 0.27
ElseIf txtCPFContribution.Text = 12.5 Then
txtCPFDeduction.Text = txtBasicSalary.Text * 0.185
ElseIf txtCPFContribution.Text = 7.5 Then
txtCPFDeduction.Text = txtBasicSalary.Text * 0.11
ElseIf txtCPFContribution.Text = 5 Then
txtCPFDeduction.Text = txtBasicSalary.Text * 0.085
End If
txtTotalDeduction.Text = Val(txtCPFDeduction.Text) + Val(txtInsuranceDeduction)



TJohns posted 2009 Jan 11 02:59
Edit boxes contain TEXT, not numbers...VB tries to help and can sometimes convert one to the other automagically, but don't trust/rely on it (number 12 comes after 9, but the text "12" comes before "9" alphabetically). Always use the VAL function (as you have in some cases) when you need the NUMBER version of a text box value. To put a number back into a text box, I think you need to use the STR() function to convert it to text first

Trevor



emogal posted 2009 Jan 11 03:18
This is the example for the CPF code....





IKnowNothing posted 2009 Jan 11 04:12
STR() will convert a number to a string but can be a bit crude and occasionally you get silly numbers like 2.999999999999999999999 instead of 3 dur to rounding errors.

Format() does the same except (as the name suggests) you can specify what the number will look like as a string. It's especially useful for decimals, insertion of thousand separators and dates
The format is quite simple Format(Target, "###") will ogive you a 3 digit string of number target Format(Target, "###.#") will force 1 (and only 1) decimal place with correct rounding so 1.66 becomes 1.7 etc.

Have a look here http://www.apostate.com/vb-format-syntax for more

Also (as an aside) have you validated the user input (ISNUMERIC is useful) to ensure a number is entered. str("f") is 0 which is not always what you want :-)




Login/Register to our forum to be able to post here.








DVDFab DVD to DVD and Blu-ray to Blu-ray offers a 20% discount until Nov 22, 2009. More info or download trial!
About   Advertise   Forum Archive   RSS Feeds   Statistics