Hi,

I'm trying to finish a college project for tommorrow, but am having real problems with my code. Every time I try and run it, I get an error message saying 'Expected If'. I looked and looked at it, but can't for the life of me work out whatthe problem is!! I would be very grateful if someone would take a look and solve the mystery for me! I've put an * where I think the problem is. Thank you. (The code is very clumsy in places, but unfortunately I'm on a tight deadline!)

<html>


<head>
<title>Organism</title>



<style type="text/css">


html {height:100%;}
body {height:100%; margin:0; padding:0;}
#bg {position:absolute; top:0; left:0; width:100%; height:100%;}
#content {position:relative; z-index:1;}


</style>


<script language="vbscript">


Option Explicit


Dim Org1Pic(9)
Dim Org2Pic(9)
Dim Org3Pic(9)
Dim x
x = 9
Dim y
y = 0
Dim noOfMoves
Dim s
s = 0
Dim n
n = 0
Dim Av
Dim Luck
Dim RandomOrg
RandomOrg = 0


Sub Window_OnLoad()


startbtn.style.pixelLeft = document.body.clientwidth / 30
startbtn.style.pixeltop = document.body.clientheight / 1.6
Org.style.pixelLeft = document.body.clientwidth / 15
Org.style.pixeltop = document.body.clientheight / 1.25
Luck1.style.pixelTop = (document.body.clientheight / 2.3) - (Luck1.height * 2)
Luck2.style.pixelTop = (document.body.clientheight / 2.3) - Luck2.height
Luck3.style.pixelTop = document.body.clientheight / 2.3
Randomize
noOfMoves = (document.body.clientwidth / 1.8) - (document.body.clientwidth / 6) / 5
p.innertext = noOfMoves
checkDocWidDiv.innertext = CInt (document.body.clientwidth / 1.8)



if n = 0 Then


EntLuck.innertext = "Please enter luck"


End if


Org1Pic(1) = "Org Gradients/Org11.gif"
Org1Pic(2) = "Org Gradients/Org10.gif"
Org1Pic(3) = "Org Gradients/Org9.gif"
Org1Pic(4) = "Org Gradients/Org8.gif"
Org1Pic(5) = "Org Gradients/Org7.gif"
Org1Pic(6) = "Org Gradients/Org6.gif"
Org1Pic(7) = "Org Gradients/Org5.gif"
Org1Pic(8) = "Org Gradients/Org4.gif"
Org1Pic(9) = "Org Gradients/Org3.gif"


Org2Pic(1) = "2ndOrgGradients/2ndOrg9.gif"
Org2Pic(2) = "2ndOrgGradients/2ndOrg8.gif"
Org2Pic(3) = "2ndOrgGradients/2ndOrg7.gif"
Org2Pic(4) = "2ndOrgGradients/2ndOrg6.gif"
Org2Pic(5) = "2ndOrgGradients/2ndOrg5.gif"
Org2Pic(6) = "2ndOrgGradients/2ndOrg4.gif"
Org2Pic(7) = "2ndOrgGradients/2ndOrg3.gif"
Org2Pic(8) = "2ndOrgGradients/2ndOrg2.gif"
Org2Pic(9) = "2ndOrgGradients/2ndOrg1.gif"


Org3Pic(1) = "2ndOrgGradients/3rdOrg9.gif"
Org3Pic(2) = "2ndOrgGradients/3rdOrg8.gif"
Org3Pic(3) = "2ndOrgGradients/3rdOrg7.gif"
Org3Pic(4) = "2ndOrgGradients/3rdOrg6.gif"
Org3Pic(5) = "2ndOrgGradients/3rdOrg5.gif"
Org3Pic(6) = "2ndOrgGradients/3rdOrg4.gif"
Org3Pic(7) = "2ndOrgGradients/3rdOrg3.gif"
Org3Pic(8) = "2ndOrgGradients/3rdOrg2.gif"
Org3Pic(9) = "2ndOrgGradients/3rdOrg1.gif"


End Sub


Sub Luck1_OnMouseOver()
Luck1.src="Luck Buttons/Luck1Over.gif"
End Sub


Sub Luck1_OnMouseOut()
Luck1.src="Luck Buttons/Luck1.gif"
End Sub


Sub Luck1_OnClick()
Luck = 1
Luck1.src="Luck Buttons/Luck1OnClick.gif"
End Sub


Sub Luck2_OnMouseOver()
Luck2.src="Luck Buttons/Luck2Over.gif"
End Sub


Sub Luck2_OnMouseOut()
Luck2.src="Luck Buttons/Luck2.gif"
End Sub


Sub Luck2_OnClick()
Luck = 5
Luck2.src="Luck Buttons/Luck2OnClick.gif"
End Sub


Sub Luck3_OnMouseOver()
Luck3.src="Luck Buttons/Luck3Over.gif"
End Sub


Sub Luck3_OnMouseOut()
Luck3.src="Luck Buttons/Luck3.gif"
End Sub


Sub Luck3_OnClick()
Luck = 9
Luck3.src="Luck Buttons/Luck3OnClick.gif"
End Sub


Sub startbtn_OnClick()


Window.SetInterval"MoveOrgRight", 20


x = Luck


RandomOrg = 1 + CInt(Rnd() * 2)


if RandomOrg = 1 Then                  * I think the problem may be in this if staement.
Org.src = Org1Pic(Luck)
Else if RandomOrg = 2 Then
Org.src = Org2Pic(Luck)
Else if RandomOrg = 3 Then
Org.src = Org3Pic(Luck)
End if


End Sub


Sub MoveOrgRight()


if CInt(Org.style.pixelLeft) < CInt (document.body.clientwidth / 1.3) Then
Org.style.pixelLeft = Org.style.pixelLeft + 1
End if


if  x <= 3 And CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2)  Then
generateLowX
Elseif x > 3 And x <= 6 And CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2) Then
generateMedX
Elseif x > 6 And x <= 9 And CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 4) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 3) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2.5) Or CInt(Org.style.pixelLeft) = CInt(document.body.clientwidth / 2) Then
generateHighX
End if


End Sub


Sub generateLowX()


n = entluck.value
x = 1 + CInt(Rnd() * 2)
y = x + y
xVal.innertext = x
yVal.innertext = y
s = s + 1
Org.src = OrgPic(Av)


ss.innertext = s
Av = CInt(y / s)
average.innertext = y / s


End Sub


Sub generateMedX()


n = entluck.value
x = 1 + CInt(Rnd() * 5)
y = x + y
xVal.innertext = x
yVal.innertext = y
s = s + 1
Org.src = OrgPic(Av)


ss.innertext = s
Av = CInt(y / s)
average.innertext = y / s


End Sub



Sub generateHighX()


n = entluck.value
x = 1 + CInt(Rnd() * 8)
y = x + y
xVal.innertext = x
yVal.innertext = y
s = s + 1
Org.src = OrgPic(Av)


ss.innertext = s
Av = CInt(y / s)
average.innertext = y / s


End Sub



Sub checkLuck_OnClick()


n = entluck.value
LuckRes.innertext = n


End Sub


</script>


</head>


<body bgcolor="000000">
<div id="bg"><img id="backGround" src="OrgBackG.jpg" width="100%" height="100%"/></div>
<div id="content">
<img id="startbtn" style="position: absolute" src="startBtn.jpg" height="25"/>
<img id="Org" style="position: absolute" src="Org Gradients/Org1.gif" height="60"/>
<img id="Luck1" style="position: absolute" src="Luck Buttons/Luck1.gif" height="40"/>
<img id="Luck2" style="position: absolute" src="Luck Buttons/Luck2.gif"height="40"/>
<img id="Luck3" style="position: absolute" src="Luck Buttons/Luck3.gif" height="40"/>
Random number:<input type="text" id="xVal" style="width:auto;"/>
Number total:<input type="text" id="yVal" style="width:auto;"/>
Number of moves?<input type="text" id="p" style="width:auto;" />
Number of moves?<input type="text" id="ss" style="width:auto;"/>
Average:<input type="text" id="average" style="width:auto;"/><br/>
<input type="text" id="Entluck"/><input type="button" id="checkLuck" value="Check Luck"/><input type="text" id="LuckRes"/><br/>
Clientwidth / 1.8:<input type="text" id="checkDocWidDiv"/><br/>


</div>
</body>



</html>

When does it stop, on what line

Hi,

Whatever code you have pointed, I guess, there should not be any space between "Else" and "If"..
Check this :

if RandomOrg = 1 Then 
   Org.src = Org1Pic(Luck)
Elseif RandomOrg = 2 Then
   Org.src = Org2Pic(Luck)
Elseif RandomOrg = 3 Then
   Org.src = Org3Pic(Luck)
End if

Regards
Veena

There are other alternatives to the end if, elseif,else if, and if statments,I have in the past used the same as a toolbar code is in a browser to handle events
case and select case is another way to perform events

example:

Dim Whatever as Integer
Select Case whatever
case whatever = somthing
case whatever could do somthing here
case else could be somthing else here
End Select
End Sub

this could be used instead of else if statments,Your going to need to play with it

<snip plug>

Thank you all for your help!

The solution was as simple as taking out the space between the 'Else' and the 'If'. I spent ages trying to work it out, but sometimes you just can't see for looking!

Thanks,

Kate

Best practice in coding is using a "tab spaces" and align the conditional statement so that it can be easily to read and debug... as what example did of QVeen

agreed i use tab spaces automatically ,i also added mouse scroll dll to be loaded aswell,seeing vb ignores the mouse wheel while loaded

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.