problem in if statement (1 Viewer)

edreamers

Registered User.
Local time
Tomorrow, 03:38
Joined
Nov 28, 2006
Messages
13
i am working on a tax program. i write the following code in the OnUpdate Event of a text field.

Private Sub Combo232_AfterUpdate()
If ([sex] = "MALE" And [total_income] > 110000 And [total_income] <= 150000) Then
Me.taxOnIncome = (([total_income] - 110000) * 10 / 100)
Else
Me.taxOnIncome = 0
End If
If ([sex] = "MALE" And [total_income] > 150000 And [total_income] <= 250000) Then
Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 4000
Else
If ([sex] = "MALE" And [total_income] > 250000) Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 24000
End If
End If

If ([sex] = "FEMALE" And [total_income] > 145000 And [total_income] <= 150000) Then
Me.taxOnIncome = (([total_income] - 145000) * 10 / 100)
Else
Me.taxOnIncome = 0
End If
If ([sex] = "FEMALE" And [total_income] > 150000 And [total_income] <= 250000) Then
Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 500
Else
If ([sex] = "FEMALE" And [total_income] > 250000) Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 20500
End If
End If

If ([sex] = "SR. CITIZEN" And [total_income] > 195000 And [total_income] <= 250000) Then
Me.taxOnIncome = (([total_income] - 195000) * 20 / 100)
Else
Me.taxOnIncome = 0
End If
If ([sex] = "SR. CITIZEN" And [total_income] > 250000) Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 11000
End If

my problem is that after completing this prog, it calculates the result only for the last part i.e. Sr. Citizen. but when i remove the "Sr. Citezen" part then it gives the results only for "Women " part. But when i remove both "women" and "Sr. citezen" part then it gives results for "Male" part.

I want all these 3 fields working. Please help me.
 
You could table drive this with 0 if statements.

You should use nested if statements. Try something like this:
Code:
If sex = "MALE" then
[INDENT]If total_income > 110000 and total_income <=  150000 then
Me.taxOnIncome = (([total_income] - 110000) * 10 / 100)
ElseIf [total_income] > 150000 And [total_income] <= 250000 Then
Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 4000
ElseIf [total_income] > 250000 Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 24000
Else
Me.taxOnIncome = 0
End If 'Male[/INDENT]ElseIf sex = "FEMALE" then
[INDENT]If [total_income] > 145000 And [total_income] <= 150000 Then
Me.taxOnIncome = (([total_income] - 145000) * 10 / 100)
ElseIf [total_income] > 250000 Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 20500
ElseIf [total_income] > 150000 And [total_income] <= 250000 Then
Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 500
Else
Me.taxOnIncome = 0
End If 'Female[/INDENT]ElseIf sex = "SR. CITIZEN" then 'Sr. citizen is a sex?
[INDENT]If [total_income] > 195000 And [total_income] <= 250000 Then
Me.taxOnIncome = (([total_income] - 195000) * 20 / 100)
ElseIf [total_income] > 250000 Then
Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 11000
End If
Else
Me.taxOnIncome = 0
End If 'Sr. Citizen[/INDENT]Else 'Not Male, Female, or Sr. Citizen
MsgBox "Error"
End If

You'll need to double-check the formulas and syntax.

Hope this helps.
 
in yuor logic you say

if sex=male and blah blah then

else

end if

if sex=female and blah blah then

else

end if

if sex=srcitizen and blah blah then

else

end if

so in all cases your code first

executes something in the male block then
something in the female block then
something in the sr ctizen block - which is what you are observing

-- you need to rethink the flow to get it manage the choices correctly
 
No, the user will select one option. i.e Male, Female or Sr. Citizen. And depend on its choice only one result will be displayed.
 
Thanks george, I will try your code and tell the result.
 
its not what you think its doing, its what it IS doing.

George's changes rectified this by putting elseifs in between the different elections

-------
you were saying

if male then
do something
else
do somrthing else
end if

if female then
do something
else
do something else
end if

if senior then
do something
else
do somrthing else
end if

so for any result you were exectuing the relevant bit of each section which is not likely to produce the desired result.

George's response may produce the right flow, but there may be a few missing end ifs - what you are aiming for is

if male then
in some cases do something
in other cases do something else
end if

if female then
in some cases do something
in other cases do something else
end if

if senior then
in some cases do something
in other cases do something else
end if

even this may be somewhat illogical - can you be senior without being male or female?
 
George's response may produce the right flow, but there may be a few missing end ifs

Actually, my original solution (with nested if statements) had an extra End If. Here is the corrected code:

Code:
Sub test()
Dim sex As String
Dim total_income As Long

If sex = "MALE" Then
    If total_income > 110000 And total_income <= 150000 Then
        Me.taxOnIncome = (([total_income] - 110000) * 10 / 100)
    ElseIf [total_income] > 150000 And [total_income] <= 250000 Then
        Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 4000
    ElseIf [total_income] > 250000 Then
        Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 24000
    Else
        Me.taxOnIncome = 0
    End If 'Male
ElseIf sex = "FEMALE" Then
    If [total_income] > 145000 And [total_income] <= 150000 Then
        Me.taxOnIncome = (([total_income] - 145000) * 10 / 100)
    ElseIf [total_income] > 250000 Then
        Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 20500
    ElseIf [total_income] > 150000 And [total_income] <= 250000 Then
        Me.taxOnIncome = (([total_income] - 150000) * 20 / 100) + 500
    Else
        Me.taxOnIncome = 0
    End If 'Female
ElseIf sex = "SR. CITIZEN" Then  'Sr. citizen is a sex?
    If [total_income] > 195000 And [total_income] <= 250000 Then
        Me.taxOnIncome = (([total_income] - 195000) * 20 / 100)
    ElseIf [total_income] > 250000 Then
        Me.taxOnIncome = (([total_income] - 250000) * 30 / 100) + 11000
    Else
        Me.taxOnIncome = 0
    End If 'Sr. Citizen
Else 'Not Male, Female, or Sr. Citizen
    MsgBox "Error"
End If
End Sub

I just tried it out and it works. Can't vouch for the calcualtions, though.

You could also:
1. Use case statements (easier to read).
2. Put all your criteria in a table and do it in one line of SQL instead of all these lines of code.
3. Use my code without the "ElseIf" between each "gender", as pointed out by GTH. The results will be identical.

The important thing is that you read up on program flow, as alluded to by GTH.
 
Thanks a lot george. The code is working well in my prog. you solved my problem. Thanks a lot.
 

Users who are viewing this thread

Back
Top Bottom