1.11M Members

if and else statement problem

 
0
 

!!

i was trying to create a simple grading system but my project doesnt work it always shows the last if statement and ignore the other statement...please help 

here's my code...can someone please tell me what's wrong coz i dont know how to use if/elseif/else statement...
newbie here...please help


Private Sub Command1_Click()
Text6.Text = Val(Text2.Text) * 0.1
Text7.Text = Val(Text3.Text) * 0.2
Text8.Text = Val(Text4.Text) * 0.3
Text9.Text = Val(Text5.Text) * 0.4
Text10.Text = Val(Text6.Text) + Val(Text7.Text) + Val(Text8.Text) + Val(Text9.Text)

If Val(Text10.Text) <= 100 Or Val(Text10.Text) >= 97 Then
Text11.Text = 1
End If
If Val(Text10.Text) <= 96 Or Val(Text10.Text) >= 91 Then
Text11.Text = 1.25
End If
If Val(Text10.Text) <= 90 Or Val(Text10.Text) >= 85 Then
Text11.Text = 1.5
End If
If Val(Text10.Text) <= 84 Or Val(Text10.Text) >= 79 Then
Text11.Text = 1.75
End If
If Val(Text10.Text) <= 78 Or Val(Text10.Text) >= 73 Then
Text11.Text = 2
End If
If Val(Text10.Text) <= 72 Or Val(Text10.Text) >= 67 Then
Text11.Text = 2.25
End If
If Val(Text10.Text) <= 66 Or Val(Text10.Text) >= 61 Then
Text11.Text = 2.5
End If
If Val(Text10.Text) <= 60 Or Val(Text10.Text) >= 55 Then
Text11.Text = 2.75
End If
If Val(Text10.Text) <= 54 Or Val(Text10.Text) >= 50 Then
Text11.Text = 3
End If
If Val(Text10.Text) <= 49 Or Val(Text10.Text) >= 25 Then
Text11.Text = 4
End If
If Val(Text10.Text) <= 24 Or Val(Text10.Text) >= 0 Then
Text11.Text = 5
End If
End Sub
 
0
 

It should work if you change all your Or's to Ands(i.e If Val(Text10.Text) <= 100 And Val(Text10.Text) >= 97 Then).

Altenatively you might find this quite a bit simpler:

Dim Score As Integer
Score = Val(Text10.Text)
If Score <= 100 Then
    If Score >= 97 Then
        Text11.Text = 1
    ElseIf Score >= 91 Then
        Text11.Text = 1.25
    ElseIf Score >= 85 Then
        Text11.Text = 1.5
    ElseIf Score >= 79 Then
        Text11.Text = 1.75
    ElseIf Score >= 73 Then
        Text11.Text = 2
    ElseIf Score >= 67 Then
        Text11.Text = 2.25
    ElseIf Score >= 61 Then
        Text11.Text = 2.5
    ElseIf Score >= 55 Then
        Text11.Text = 2.75
    ElseIf Score >= 50 Then
        Text11.Text = 3
    ElseIf Score >= 25 Then
        Text11.Text = 4
    Else
        Text11.Text = 5
    End If
End If

You also could use a Select-Case block:

If Score <= 100 Then
    Select Case Score
        Case Score >= 97
            Text11.Text = 1
        Case Score >= 91
            Text11.Text = 1.25
    End Select
End If
 
0
 

thank you very much Mr. tinstaafl...can i have one last question...

in the other textboxes, how to put a range in it...the only range that will accept is 0-100
and if the user input a negative number or greater than 100 it will not accept it....
hoping for your help...

THANK YOU AGAIN!! much appreciated...

Question Answered as of 1 Year Ago by tinstaafl
 
0
 

Use the Change event(double-click on the textbox to create the code stub to handle the Change event). Then something like this in the sub routine:

Dim Value as Integer
Value = Val(Text2.Text)
If Value > 100 Or Value < 0 Then
    Text2.Text = ""
    Msgbox "Value must be between 0 and 100 inclusive"
End If
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: