12 Replies - 458 Views - Last Post: 15 August 2011 - 08:33 AM Rate Topic: -----

#1 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

VB8 problem with IF statement

Posted 14 August 2011 - 02:08 PM

Hi I am new to VB8 and I cannot get the IF statement right.I think I am not getting the Dim correct.
I have posted before with an IF problem and this program is ok until the last IF.
Any help appricated.

jones246

'Create a program the calculates an employee's USC(if they earn more thatn €4004),based on the rates below.
'€0-€10,036 is 2%
'€10,036.01 to €16016 is 4%
'Above €16.016 is 7%

Public Class frm_universial_charge
    Dim charge As Double
    Dim income As Double
    'Dim income As Double
    Dim display_charge_result As Double
    Dim strFormattedAmount As String

    Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If txt_input_income.Text <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf txt_input_income.Text <= 10036.0 Then
            lbl_display_charge_result.Text = (txt_input_income.Text / 100) * 0.2
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf txt_input_income.Text >= 10036.01 <= 16016.0 Then
            lbl_display_charge_result.Text = (txt_input_income.Text / 100) * 0.4
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf txt_input_income.Text = (txt_input_income.Text / 100) * 0.7 Then
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Red
        lbl_rate.ForeColor = Color.Red
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_input_income.Text = ""
        lbl_display_charge_result.Text = ""
        lbl_rate.Text = ""
    End Sub

    Private Sub frm_universial_charge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

This post has been edited by AdamSpeight2008: 14 August 2011 - 07:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: VB8 problem with IF statement

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: VB8 problem with IF statement

Posted 14 August 2011 - 02:36 PM

First: your last ifElse has problem:
 ElseIf txt_input_income.Text = (txt_input_income.Text / 100) * 0.7 Then
lbl_rate.Text = "Your USC rate is 7%"
End If 

you have to test if input is greater than 16016.

Second: this case;
 ElseIf txt_input_income.Text >= 10036.01 <= 16016.0 Then 

it is better if you have AndAlso. Like
 ElseIf Val(txt_input_income.Text) >= 10036.01 AndAlso Val(txt_input_income.Text) <= 16016.0 Then 


Third: The value from text boxes are strings and it is not good practice to use as a number. Either use Val() function or use Parse() to change it to real number.

Last: I doubt your calculation!!! why you divide by 100 and then times 0.?

Also :code:
Was This Post Helpful? 0
  • +
  • -

#3 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 25-May 11

Re: VB8 problem with IF statement

Posted 14 August 2011 - 02:37 PM

I think your problem would be trying to directly get information from a text file from within a IF statement.
Look into Stream.Read: http://msdn.microsof...tream.read.aspx
Was This Post Helpful? 0
  • +
  • -

#4 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

Re: VB8 problem with IF statement

Posted 14 August 2011 - 03:39 PM

Hi Smohd

This calculation should IF i have it correct return a 7% of total txt_input_income.Text / 100) * 0.7

 Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If Val(txt_input_income.Text) <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf Val(txt_input_income.Text) > 4004.0 AndAlso Val(txt_input_income.Text) <= 10036.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.2
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf Val(txt_input_income.Text) > 16016.0 AndAlso Val(txt_input_income.Text) <= 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.4
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf Val(txt_input_income.Text) > 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.7
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Blue
        lbl_rate.ForeColor = Color.Red
    End Sub

Now the first an last IF works but nor the second and third

Thanks for your time

jones246

This post has been edited by AdamSpeight2008: 14 August 2011 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: VB8 problem with IF statement

Posted 14 August 2011 - 03:53 PM

The calculation should be:
 Val(txt_input_income.Text / 100) * 7
this will give 7% and not * 0.7. Or simply
 Val(txt_input_income.Text * 0.07


What you mean by it doesnt work? What happens when you enter value that is in that range?

EDIT:
this ifelse is logical error:
 ElseIf Val(txt_input_income.Text) > 16016.0 AndAlso Val(txt_input_income.Text) <= 16016.0 

It should be 10036 to 16016

:code:

This post has been edited by smohd: 14 August 2011 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

Re: VB8 problem with IF statement

Posted 14 August 2011 - 04:11 PM

Smohd
Thanks for your help.
The first IF returns both text boxes and the last IF returns both text boxes but the second and third return IFs return 2 blank text boxes.

Public Class frm_universial_charge
    Dim charge As Double
    Dim income As Double
    Dim display_charge_result As Double
    Dim strFormattedAmount As String

    Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If Val(txt_input_income.Text) <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf Val(txt_input_income.Text) > 4004.0 AndAlso Val(txt_input_income.Text) <= 10036.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.2
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf Val(txt_input_income.Text) > 10036.0 AndAlso Val(txt_input_income.Text) <= 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.4
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf Val(txt_input_income.Text) > 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.7
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Blue
        lbl_rate.ForeColor = Color.Red
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_input_income.Text = ""
        lbl_display_charge_result.Text = ""
        lbl_rate.Text = ""
    End Sub

    Private Sub frm_universial_charge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub lbl_show_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_show_charge.Click

    End Sub
End Class


Thanks

jones246

This post has been edited by AdamSpeight2008: 14 August 2011 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: VB8 problem with IF statement

Posted 14 August 2011 - 04:26 PM

:code: please.
I dont get why that should happen, may be what value have you tried that gives that error
Was This Post Helpful? 0
  • +
  • -

#8 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

Re: VB8 problem with IF statement

Posted 14 August 2011 - 04:52 PM

Hi Smohd
I cannot see what the problem is but I still think a DIM is wrong.
Thanks for your help
Regards jones246

Create a program the calculates an employee's USC(if they earn more thatn €4004),based on the rates below.
'€0-€10,036 is 2%
'€10,036.01 to €16,016 is 4%
'Above €16,016.0 is 7%

Public Class frm_universial_charge
    Dim charge As Double
    Dim income As Double
    Dim display_charge_result As Double
    Dim strFormattedAmount As String

    Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If Val(txt_input_income.Text) <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf Val(txt_input_income.Text) > 4004.0 <= 10036.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.2
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf Val(txt_input_income.Text) > 10036.0 AndAlso Val(txt_input_income.Text) <= 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.4
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf Val(txt_input_income.Text) > 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.7
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Blue
        lbl_rate.ForeColor = Color.Red
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_input_income.Text = ""
        lbl_display_charge_result.Text = ""
        lbl_rate.Text = ""
    End Sub
    Private Sub frm_universial_charge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub lbl_show_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_show_charge.Click

    End Sub
End Class 

This post has been edited by AdamSpeight2008: 14 August 2011 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: VB8 problem with IF statement

Posted 14 August 2011 - 04:56 PM

I mean what value you put in txt_input_income that course that problem during testing?
Was This Post Helpful? 0
  • +
  • -

#10 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

Re: VB8 problem with IF statement

Posted 14 August 2011 - 05:03 PM

Hi Smohd

I place values higher or lower that the IF statement to test the output.

I cannot understand why IF statement 1 and 4 work but 2 and 3 are blank.

Regards jones246
Was This Post Helpful? 0
  • +
  • -

#11 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: VB8 problem with IF statement

Posted 14 August 2011 - 07:41 PM

Hi Jones246 i am not an expert and i am awful at explaining things to people so am not going to try i had look at that for you came up with this.I have not fully checked this but its seems to work for me have a look and if there is any thing you don't understand ask smohd to explain.

    Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If Val(txt_input_income.Text) <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf CBool(CDbl(Val(txt_input_income.Text) > 4004.0 AndAlso Val(txt_input_income.Text) <= 10036.0)) Then
            lbl_display_charge_result.Text = CStr(CDbl(txt_input_income.Text) / 100)
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf Val(txt_input_income.Text) > 10036.0 AndAlso Val(txt_input_income.Text) <= 16016.0 Then
            lbl_display_charge_result.Text = CStr((CDbl(txt_input_income.Text) / 100) * 0.4)
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf Val(txt_input_income.Text) > 16016.0 Then
            lbl_display_charge_result.Text = CStr((CDbl(txt_input_income.Text) / 100) * 0.7)
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Blue
        lbl_rate.ForeColor = Color.Red
    End Sub

Was This Post Helpful? 0
  • +
  • -

#12 jones246  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-August 11

Re: VB8 problem with IF statement

Posted 15 August 2011 - 03:38 AM

Hi m_wylie85

The code is not working properly.

Any value over the 2% limits is still returning 2%.

It is a if the first 2 IF statements are being acted upon and the second IF statements are not acted on.

Regards

jones246

'Create a program the calculates an employee's USC(if they earn more thatn 4004),based on the rates below.
'0-10,036 is 2%
'10,036.01 to 16,016 is 4%
'Above 16,016.0 is 7%

Public Class frm_universial_charge
    Dim charge As Double
    Dim income As Double
    Dim display_charge_result As Double
    Dim strFormattedAmount As String

    Private Sub btn_calculate_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calculate_charge.Click
        If Val(txt_input_income.Text) <= 4004.0 Then
            lbl_display_charge_result.Text = "Congratulations you are exempt from the Universal Social Charge."
            lbl_rate.Text = "You are under the USC income rate,"
        ElseIf Val(txt_input_income.Text) > 4004.0 <= 10036.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.2
            lbl_rate.Text = "Your USC rate is 2%"
        ElseIf Val(txt_input_income.Text) > 10036.0 AndAlso Val(txt_input_income.Text) <= 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.4
            lbl_rate.Text = "Your USC rate is 4%"
        ElseIf Val(txt_input_income.Text) > 16016.0 Then
            lbl_display_charge_result.Text = Val(txt_input_income.Text / 100) * 0.7
            lbl_rate.Text = "Your USC rate is 7%"
        End If
        lbl_display_charge_result.ForeColor = Color.Blue
        lbl_rate.ForeColor = Color.Red
    End Sub

    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_input_income.Text = ""
        lbl_display_charge_result.Text = ""
        lbl_rate.Text = ""
    End Sub

    Private Sub frm_universial_charge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub lbl_show_charge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_show_charge.Click

    End Sub
End Class


Mentor Edit: Learn to use the code tags please. DIC Team Blog: Code Tags

This post has been edited by AdamSpeight2008: 15 August 2011 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#13 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: VB8 problem with IF statement

Posted 15 August 2011 - 08:33 AM

Hey jones246 i just checked my code at my end there and it work perfectly for me. I just had a look at the code you post above that is not the code i gave you copy my code form previous post and use that it works. you are missing a couple of things i put in like:
CBool(CDbl(Val

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1