Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Multiple If/Then Statements 3

Status
Not open for further replies.

Aleena

Programmer
Oct 3, 1999
27
US
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 &quot;Compile error: End If without Block If&quot;. 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) :)<br>
<br>
If you need to know anything else about what I am trying to accomplish here just drop me a line.<br>
<br>
Thanks,<br>
Aleena<br>
<br>
Private Sub cmdComputate_Click()<br>
picScore.Cls<br>
picGrade.Cls<br>
<br>
x = 0<br>
<br>
If cboScore1.Text &lt;&gt; &quot;N/A&quot; Then<br>
A = Val(cboScore1.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore2.Text &lt;&gt; &quot;N/A&quot; Then<br>
b = Val(cboScore2.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore3.Text &lt;&gt; &quot;N/A&quot; Then<br>
c = Val(cboScore3.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore4.Text &lt;&gt; &quot;N/A&quot; Then<br>
d = Val(cboScore4.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore5.Text &lt;&gt; &quot;N/A&quot; Then<br>
e = Val(cboScore5.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore6.Text &lt;&gt; &quot;N/A&quot; Then<br>
f = Val(cboScore6.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore7.Text &lt;&gt; &quot;N/A&quot; Then<br>
g = Val(cboScore7.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore8.Text &lt;&gt; &quot;N/A&quot; Then<br>
h = Val(cboScore8.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore9.Text &lt;&gt; &quot;N/A&quot; Then<br>
j = Val(cboScore9.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore10.Text &lt;&gt; &quot;N/A&quot; Then<br>
k = Val(cboScore10.Text)<br>
x = x + 50<br>
End If<br>
<br>
If cboScore11.Text &lt;&gt; &quot;N/A&quot; Then<br>
m = Val(cboScore11.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore12.Text &lt;&gt; &quot;N/A&quot; Then<br>
n = Val(cboScore12.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore13.Text &lt;&gt; &quot;N/A&quot; Then<br>
p = Val(cboScore13.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore14.Text &lt;&gt; &quot;N/A&quot; Then<br>
r = Val(cboScore14.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore15.Text &lt;&gt; &quot;N/A&quot; Then<br>
s = Val(cboScore15.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore16.Text &lt;&gt; &quot;N/A&quot; Then<br>
t = Val(cboScore16.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore17.Text &lt;&gt; &quot;N/A&quot; Then<br>
u = Val(cboScore17.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore18.Text &lt;&gt; &quot;N/A&quot; Then<br>
v = Val(cboScore18.Text)<br>
x = x + 10<br>
End If<br>
<br>
z = A + b + c + d + e + f + g + h + j + k + m + n + p + r + s + t + u + v<br>
q = (100 * z) \ x<br>
<br>
(Right here is where my problem develops)<br>
<br>
If q &gt;= 93 Then picGrade.Print &quot;A&quot;<br>
End If<br>
<br>
If q &gt;= 90 Then picGrade.Print &quot;A-&quot;<br>
End If<br>
<br>
If q &gt;= 88 Then picGrade.Print &quot;B+&quot;<br>
End If<br>
<br>
If q &gt;= 82 Then picGrade.Print &quot;B&quot;<br>
End If<br>
<br>
If q &gt;= 80 Then picGrade.Print &quot;B-&quot;<br>
End If<br>
<br>
If q &gt;= 78 Then picGrade.Print &quot;C+&quot;<br>
End If<br>
<br>
If q &gt;= 72 Then picGrade.Print &quot;C&quot;<br>
End If<br>
<br>
If q &gt;= 70 Then picGrade.Print &quot;C-&quot;<br>
End If<br>
<br>
If q &gt;= 68 Then picGrade.Print &quot;D+&quot;<br>
End If<br>
<br>
If q &gt;= 62 Then picGrade.Print &quot;D&quot;<br>
End If<br>
<br>
If q &gt;= 60 Then picGrade.Print &quot;D-&quot;<br>
End If<br>
<br>
If q &gt;= 1 Then picGrade.Print &quot;F&quot;<br>
End If<br>
<br>
picScore.Print q<br>
<br>
<br>
End Sub
 
Hi Aleena,<br>
Your problem is that you r writing the end if statement after the if then statement which gets finished off in a line.<br>
Your code which compiles goes like this :<br>
<br>
<br>
Private Sub Command1_Click()<br>
<br>
picScore.Cls<br>
picGrade.Cls<br>
<br>
x = 0<br>
<br>
If cboScore1.Text &lt;&gt; &quot;N/A&quot; Then<br>
A = Val(cboScore1.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore2.Text &lt;&gt; &quot;N/A&quot; Then<br>
b = Val(cboScore2.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore3.Text &lt;&gt; &quot;N/A&quot; Then<br>
c = Val(cboScore3.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore4.Text &lt;&gt; &quot;N/A&quot; Then<br>
d = Val(cboScore4.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore5.Text &lt;&gt; &quot;N/A&quot; Then<br>
e = Val(cboScore5.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore6.Text &lt;&gt; &quot;N/A&quot; Then<br>
f = Val(cboScore6.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore7.Text &lt;&gt; &quot;N/A&quot; Then<br>
g = Val(cboScore7.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore8.Text &lt;&gt; &quot;N/A&quot; Then<br>
h = Val(cboScore8.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore9.Text &lt;&gt; &quot;N/A&quot; Then<br>
j = Val(cboScore9.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore10.Text &lt;&gt; &quot;N/A&quot; Then<br>
k = Val(cboScore10.Text)<br>
x = x + 50<br>
End If<br>
<br>
If cboScore11.Text &lt;&gt; &quot;N/A&quot; Then<br>
m = Val(cboScore11.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore12.Text &lt;&gt; &quot;N/A&quot; Then<br>
n = Val(cboScore12.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore13.Text &lt;&gt; &quot;N/A&quot; Then<br>
p = Val(cboScore13.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore14.Text &lt;&gt; &quot;N/A&quot; Then<br>
r = Val(cboScore14.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore15.Text &lt;&gt; &quot;N/A&quot; Then<br>
s = Val(cboScore15.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore16.Text &lt;&gt; &quot;N/A&quot; Then<br>
t = Val(cboScore16.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore17.Text &lt;&gt; &quot;N/A&quot; Then<br>
u = Val(cboScore17.Text)<br>
x = x + 10<br>
End If<br>
<br>
If cboScore18.Text &lt;&gt; &quot;N/A&quot; Then<br>
v = Val(cboScore18.Text)<br>
x = x + 10<br>
End If<br>
<br>
z = A + b + c + d + e + f + g + h + j + k + m + n + p + r + s + t + u + v<br>
q = (100 * z) \ x<br>
<br>
'(Right here is where my problem develops)<br>
<br>
If q &gt;= 93 Then<br>
picGrade.Print &quot;A&quot;<br>
End If<br>
<br>
If q &gt;= 90 Then<br>
picGrade.Print &quot;A-&quot;<br>
End If<br>
<br>
If q &gt;= 88 Then<br>
picGrade.Print &quot;B+&quot;<br>
End If<br>
<br>
If q &gt;= 82 Then<br>
picGrade.Print &quot;B&quot;<br>
End If<br>
<br>
If q &gt;= 80 Then<br>
picGrade.Print &quot;B-&quot;<br>
End If<br>
<br>
If q &gt;= 78 Then<br>
picGrade.Print &quot;C+&quot;<br>
End If<br>
<br>
If q &gt;= 72 Then<br>
picGrade.Print &quot;C&quot;<br>
End If<br>
<br>
If q &gt;= 70 Then<br>
picGrade.Print &quot;C-&quot;<br>
End If<br>
<br>
If q &gt;= 68 Then<br>
picGrade.Print &quot;D+&quot;<br>
End If<br>
<br>
If q &gt;= 62 Then<br>
picGrade.Print &quot;D&quot;<br>
End If<br>
<br>
If q &gt;= 60 Then<br>
picGrade.Print &quot;D-&quot;<br>
End If<br>
<br>
If q &gt;= 1 Then<br>
picGrade.Print &quot;F&quot;<br>
End If<br>
<br>
picScore.Print q<br>
End Sub<br>
<br>
<br>
If you have any other probs please feel free<br>
Ravi<br>
<br>
E-Mail : rkochher@velos.ssind.com<br>
<br>
<p>Ravi Kochher<br><a href=mailto:rkochher@velos.ssind.com>rkochher@velos.ssind.com</a><br><a href= > </a><br>
 
Thanks for the help :)<br>
<br>
It works great now. I guess I just didn't format it correctly. It had me tearing my hair out for awhile there... <br>
<br>
Aleena
 
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.
 
Thanks. :)<br>
<br>
I will check it out<br>
<br>
Aleena
 
Very true about Select...End Select. It reads easier and probably runs faster.<br>
<br>
Select Case q<br>
Case 93<br>
picGrade.Print &quot;A&quot;<br>
Case 90<br>
picGrade.Print &quot;A-&quot;<br>
Case 88<br>
picGrade.Print &quot;B+&quot;<br>
Case 82<br>
picGrade.Print &quot;B&quot;<br>
Case 80<br>
picGrade.Print &quot;B-&quot;<br>
'..... and so on<br>
End Select<br>
Now, when we are able to tab-indent on Tek-Tips, the code will be even easier to read.<br>

 
BTW, throw on a &quot;Case Else&quot; as your last case whenever you need to catch unexpected values (like &lt;1, &gt;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 &quot;end case&quot;, so it is faster as well as more readable. I almost always use case stmts rather than ifs/nested ifs.
 
lol !! You know what, I developed that very problem (unexpected values). I think I will recode it all to the select case.<br>
<br>
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.<br>
<br>
Thanks again Elizabeth :)<br>
<br>
Aleena<br>
<br>

 
While your at it...<br>
<br>
Consider creating a control array. When designing your form, add your combo box and call it cboScore (no number)<br>
<br>
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).<br>
<br>
Then, in code you can replace the 18 if statements:<br>
<br>
If cboScoreNN.Text &lt;&gt; &quot;N/A&quot; Then<br>
t = Val(cboScoreNN.Text)<br>
x = x + 10<br>
End If<br>
<br>
with a single if statement within a loop:<br>
<br>
for i = 1 to 18<br>
If cboScore(i).Text &lt;&gt; &quot;N/A&quot; Then<br>
z = z + Val(cboScore(i).Text)<br>
x = x + 10<br>
End If<br>
next i<br>
<br>
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:<br>
<br>
intScore(i) = Val(cboScore(i).Text)<br>

 
p.s. Aleena, you probably want to use ranges (93 to 100, 90 to 92, etc.) in your case stmts rather than single values.
 
Thanks bitbrain :) <br>
I will play around with that. I would sure make the whole thing quite a bit shorter... hehe<br>
<br>
Aleena <p>Aleena<br><a href=mailto:petersen@cdsnet.net>petersen@cdsnet.net</a><br><a href= Personal Website</a><br>"I can picture a world without war, a world without hate. <br>
And I can picture us attacking this world, because they'd never expect it"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top