×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help with Multiple If/Then Statements
3

Help with Multiple If/Then Statements

Help with Multiple If/Then Statements

(OP)
I am new to Visual Basic and I am having a difficulty setting multiple strings of If/Then statements for a command buttons click procedure. I know I must be missing something or have something in the wrong order. The first set of if statements works fine but my second set gives me a "Compile error: End If without Block If". As I have all my If's paired with then's and end if's, I don't understand what is wrong. Maybe one of you experts out there can take a look at the following code and give me some advice (Please O Please) :-)

If you need to know anything else about what I am trying to accomplish here just drop me a line.

Thanks,
Aleena

Private Sub cmdComputate_Click()
picScore.Cls
picGrade.Cls

x = 0

If cboScore1.Text <> "N/A" Then
A = Val(cboScore1.Text)
x = x + 10
End If

If cboScore2.Text <> "N/A" Then
b = Val(cboScore2.Text)
x = x + 10
End If

If cboScore3.Text <> "N/A" Then
c = Val(cboScore3.Text)
x = x + 10
End If

If cboScore4.Text <> "N/A" Then
d = Val(cboScore4.Text)
x = x + 10
End If

If cboScore5.Text <> "N/A" Then
e = Val(cboScore5.Text)
x = x + 10
End If

If cboScore6.Text <> "N/A" Then
f = Val(cboScore6.Text)
x = x + 10
End If

If cboScore7.Text <> "N/A" Then
g = Val(cboScore7.Text)
x = x + 10
End If

If cboScore8.Text <> "N/A" Then
h = Val(cboScore8.Text)
x = x + 10
End If

If cboScore9.Text <> "N/A" Then
j = Val(cboScore9.Text)
x = x + 10
End If

If cboScore10.Text <> "N/A" Then
k = Val(cboScore10.Text)
x = x + 50
End If

If cboScore11.Text <> "N/A" Then
m = Val(cboScore11.Text)
x = x + 10
End If

If cboScore12.Text <> "N/A" Then
n = Val(cboScore12.Text)
x = x + 10
End If

If cboScore13.Text <> "N/A" Then
p = Val(cboScore13.Text)
x = x + 10
End If

If cboScore14.Text <> "N/A" Then
r = Val(cboScore14.Text)
x = x + 10
End If

If cboScore15.Text <> "N/A" Then
s = Val(cboScore15.Text)
x = x + 10
End If

If cboScore16.Text <> "N/A" Then
t = Val(cboScore16.Text)
x = x + 10
End If

If cboScore17.Text <> "N/A" Then
u = Val(cboScore17.Text)
x = x + 10
End If

If cboScore18.Text <> "N/A" Then
v = Val(cboScore18.Text)
x = x + 10
End If

z = A + b + c + d + e + f + g + h + j + k + m + n + p + r + s + t + u + v
q = (100 * z) \ x

(Right here is where my problem develops)

If q >= 93 Then picGrade.Print "A"
End If

If q >= 90 Then picGrade.Print "A-"
End If

If q >= 88 Then picGrade.Print "B+"
End If

If q >= 82 Then picGrade.Print "B"
End If

If q >= 80 Then picGrade.Print "B-"
End If

If q >= 78 Then picGrade.Print "C+"
End If

If q >= 72 Then picGrade.Print "C"
End If

If q >= 70 Then picGrade.Print "C-"
End If

If q >= 68 Then picGrade.Print "D+"
End If

If q >= 62 Then picGrade.Print "D"
End If

If q >= 60 Then picGrade.Print "D-"
End If

If q >= 1 Then picGrade.Print "F"
End If

picScore.Print q


End Sub

RE: Help with Multiple If/Then Statements

Hi Aleena,
Your problem is that you r writing the end if statement after the if then statement which gets finished off in a line.
Your code which compiles goes like this :


Private Sub Command1_Click()

picScore.Cls
picGrade.Cls

x = 0

If cboScore1.Text <> "N/A" Then
A = Val(cboScore1.Text)
x = x + 10
End If

If cboScore2.Text <> "N/A" Then
b = Val(cboScore2.Text)
x = x + 10
End If

If cboScore3.Text <> "N/A" Then
c = Val(cboScore3.Text)
x = x + 10
End If

If cboScore4.Text <> "N/A" Then
d = Val(cboScore4.Text)
x = x + 10
End If

If cboScore5.Text <> "N/A" Then
e = Val(cboScore5.Text)
x = x + 10
End If

If cboScore6.Text <> "N/A" Then
f = Val(cboScore6.Text)
x = x + 10
End If

If cboScore7.Text <> "N/A" Then
g = Val(cboScore7.Text)
x = x + 10
End If

If cboScore8.Text <> "N/A" Then
h = Val(cboScore8.Text)
x = x + 10
End If

If cboScore9.Text <> "N/A" Then
j = Val(cboScore9.Text)
x = x + 10
End If

If cboScore10.Text <> "N/A" Then
k = Val(cboScore10.Text)
x = x + 50
End If

If cboScore11.Text <> "N/A" Then
m = Val(cboScore11.Text)
x = x + 10
End If

If cboScore12.Text <> "N/A" Then
n = Val(cboScore12.Text)
x = x + 10
End If

If cboScore13.Text <> "N/A" Then
p = Val(cboScore13.Text)
x = x + 10
End If

If cboScore14.Text <> "N/A" Then
r = Val(cboScore14.Text)
x = x + 10
End If

If cboScore15.Text <> "N/A" Then
s = Val(cboScore15.Text)
x = x + 10
End If

If cboScore16.Text <> "N/A" Then
t = Val(cboScore16.Text)
x = x + 10
End If

If cboScore17.Text <> "N/A" Then
u = Val(cboScore17.Text)
x = x + 10
End If

If cboScore18.Text <> "N/A" Then
v = Val(cboScore18.Text)
x = x + 10
End If

z = A + b + c + d + e + f + g + h + j + k + m + n + p + r + s + t + u + v
q = (100 * z) \ x

'(Right here is where my problem develops)

If q >= 93 Then
picGrade.Print "A"
End If

If q >= 90 Then
picGrade.Print "A-"
End If

If q >= 88 Then
picGrade.Print "B+"
End If

If q >= 82 Then
picGrade.Print "B"
End If

If q >= 80 Then
picGrade.Print "B-"
End If

If q >= 78 Then
picGrade.Print "C+"
End If

If q >= 72 Then
picGrade.Print "C"
End If

If q >= 70 Then
picGrade.Print "C-"
End If

If q >= 68 Then
picGrade.Print "D+"
End If

If q >= 62 Then
picGrade.Print "D"
End If

If q >= 60 Then
picGrade.Print "D-"
End If

If q >= 1 Then
picGrade.Print "F"
End If

picScore.Print q
End Sub


If you have any other probs please feel free
Ravi

E-Mail : rkochher@velos.ssind.com

Ravi Kochher
rkochher@velos.ssind.com

RE: Help with Multiple If/Then Statements

(OP)
Thanks for the help :-)

It works great now. I guess I just didn't format it correctly. It had me tearing my hair out for awhile there...

Aleena

RE: Help with Multiple If/Then Statements

If you're not familiar with the case stmt (Select Case... End Select) you might want to check it out. I find it a lot easier to read.

RE: Help with Multiple If/Then Statements

(OP)
Thanks. :)

I will check it out

Aleena

RE: Help with Multiple If/Then Statements

Very true about Select...End Select. It reads easier and probably runs faster.

Select Case q
Case 93
picGrade.Print "A"
Case 90
picGrade.Print "A-"
Case 88
picGrade.Print "B+"
Case 82
picGrade.Print "B"
Case 80
picGrade.Print "B-"
'..... and so on
End Select
Now, when we are able to tab-indent on Tek-Tips, the code will be even easier to read.

RE: Help with Multiple If/Then Statements

BTW, throw on a "Case Else" as your last case whenever you need to catch unexpected values (like <1, >100, null). Even if your form is checking for invalids now, you or someone else may modify your form someday and lose that safeguard. Re: the if stmts, they would be needed if the alternatives were not mutually exclusive. But once the case stmt finds a match it goes to "end case", so it is faster as well as more readable. I almost always use case stmts rather than ifs/nested ifs.

RE: Help with Multiple If/Then Statements

(OP)
lol !! You know what, I developed that very problem (unexpected values). I think I will recode it all to the select case.

You guys have been a great help. This is my sophomore year at college and I am just now starting my programming class (like just this week..hehe) and we have not discussed any of this in class. I decided to experiment on my own for a bit. (I think that is always the best way to learn something new) My text is not too specific on some items so I really appreciate all of your advice.

Thanks again Elizabeth :-)

Aleena

RE: Help with Multiple If/Then Statements

Glad to help! ;-)

RE: Help with Multiple If/Then Statements

While your at it...

Consider creating a control array. When designing your form, add your combo box and call it cboScore (no number)

Select & copy it to the clip board, then paste. VB will ask if you want to create a control array - reply yes and paste it as many times as necessary (18 in the example).

Then, in code you can replace the 18 if statements:

If cboScoreNN.Text <> "N/A" Then
t = Val(cboScoreNN.Text)
x = x + 10
End If

with a single if statement within a loop:

for i = 1 to 18
If cboScore(i).Text <> "N/A" Then
z = z + Val(cboScore(i).Text)
x = x + 10
End If
next i

if you really need to keep track of the individual score values (a,b,c...) cretae a score array (Dim intScore(18) As Integer)and use it in your if statement:

intScore(i) = Val(cboScore(i).Text)

RE: Help with Multiple If/Then Statements

p.s. Aleena, you probably want to use ranges (93 to 100, 90 to 92, etc.) in your case stmts rather than single values.

RE: Help with Multiple If/Then Statements

(OP)
Thanks bitbrain :)
I will play around with that. I would sure make the whole thing quite a bit shorter... hehe

Aleena

Aleena
petersen@cdsnet.net
My Personal Website
"I can picture a world without war, a world without hate.
And I can picture us attacking this world, because they'd never expect it"

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close