VideoHelp Forum




+ Reply to Thread
Results 1 to 4 of 4
  1. Member
    Join Date
    Jan 2009
    Location
    Singapore
    Search Comp PM
    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)
    Quote Quote  
  2. Member
    Join Date
    Jun 2004
    Location
    Victoria, Australia
    Search Comp PM
    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
    Quote Quote  
  3. Member
    Join Date
    Jan 2009
    Location
    Singapore
    Search Comp PM
    This is the example for the CPF code....


    Quote Quote  
  4. Member
    Join Date
    Feb 2007
    Location
    United Kingdom
    Search Comp PM
    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
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!