[RESOLVED] Runtime error '9': subscript out of range-VBForums
Results 1 to 17 of 17

Thread: [RESOLVED] Runtime error '9': subscript out of range

  1. #1

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Resolved [RESOLVED] Runtime error '9': subscript out of range

    Hi i have made a little application for a game i play which tells me how many players there are in each server.
    the game has 120 worlds. but 121 buttons . world 116 isnt used at all.
    My problem is i run my program with world 116 in it, i get this error Runtime error '9': subscript out of range.an it points this part of the code which is in red font colour .
    VB Code:
    1. Private Function worldplayerslist()
    2. Dim worlds(121) As String, temp() As String, temp2 As String, temp3 As String, temp4 As String
    3. On Error GoTo skip2
    4. Text2.Text = Inet1.OpenURL("http://runescape.com/serverlist.ws?plugin=0&lores.x=163&lores.y=74")
    5. DoEvents
    6. For num = 1 To 121
    7.     On Error GoTo offline
    8.     [COLOR=Red]temp2 = Left(Split(Text2.Text, "World " & num & "<")(1), 30)[/COLOR]
    9.     temp3 = InStr(1, temp2, "Players")
    10.     temp4 = InStr(1, temp2, "empty")
    11.     If (temp4 > 0) Then
    12.         temp2 = "OFFLINE"
    13.     Else
    14.         If (temp3 > 0) Then
    15.             temp2 = Split(Split(temp2, " Players")(0), "<td>")(1)
    16.         Else
    17.             temp2 = "Full"
    18.         End If
    19.     End If
    20.     Text1(num - 1).Text = temp2
    21.     GoTo skip
    22. offline:
    23.     Text1(num - 1).Text = "Offline"
    24. skip:
    25. Next num
    26. skip2:
    27. End Function
    28.  
    29. Private Sub Form_Load()
    30. frmWait.Show
    31. Me.Show
    32. frmWait.ZOrder 0
    33. Me.ZOrder 1
    34. Timer1.Enabled = False
    35. Call worldplayerslist
    36. Unload frmWait
    37. For g = 0 To 105
    38.     Text1(g).Locked = True
    39. Next g
    40. mnuStart.Enabled = True
    41. mnuStop.Enabled = False
    42. End Sub
    43.  
    44.  
    45.  
    46. Private Sub mnuExit_Click()
    47. If Timer1.Enabled = True Then
    48.     MsgBox "You must first disable the automatic refresh.", vbCritical, "Hold It Right There!"
    49. Else
    50.     Unload Me
    51. End If
    52. End Sub
    53.  
    54.  
    55.  
    56. Private Sub mnuRefresh_Click()
    57. Timer1.Enabled = False
    58. mnuStart.Enabled = True
    59. mnuStop.Enabled = False
    60. frmWait.Show
    61. Me.Show
    62. frmWait.ZOrder 0
    63. Me.ZOrder 1
    64. Timer1.Enabled = False
    65. Call worldplayerslist
    66. Unload frmWait
    67. End Sub
    68.  
    69. Private Sub mnuSet_Click()
    70. If Timer1.Enabled = True Then
    71.     MsgBox "You must first disable the automatic refresh.", vbCritical, "Hold It Right There!"
    72.     GoTo skip2
    73. End If
    74. frmSet.Show
    75. skip2:
    76. End Sub
    77.  
    78. Private Sub mnuStop_Click()
    79. Timer1.Enabled = False
    80. mnuStart.Enabled = True
    81. mnuStop.Enabled = False
    82. End Sub
    83.  
    84. Private Sub mnuStart_Click()
    85. Timer1.Enabled = True
    86. mnuStart.Enabled = False
    87. mnuStop.Enabled = True
    88. End Sub
    89.  
    90.  
    91. Private Sub Timer1_Timer()
    92. Call worldplayerslist
    93. End Sub

    Please could some one explain what i have done wrong?
    Many thanks
    Sheepy

  2. #2
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Runtime error '9': subscript out of range

    This would happen if Text2.Text did not contain the split value, which would result in a single-element array, thereby causing a reference to the second element to fail.

  3. #3

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    how could i fix this. its picking ling my head

    Many thanks

  4. #4
    Frenzied Member wiz126's Avatar
    Join Date
    Jul 2005
    Location
    Mars,Milky Way... Chit Chat Posts: 5,733
    Posts
    1,080

    Re: Runtime error '9': subscript out of range

    Quote Originally Posted by Sheepy_Daz
    how could i fix this. its picking ling my head

    Many thanks
    Don't let the user enter the world, have all the world in a combo box.

    Also (I used to play rune scape 4 years ago (I was rank top 20) Now I rank like 20k lol)
    I think this is against the rules of the game to use a software
    P.S. RS NAME USED TO BE Wiz126
    1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
    2) If someone has been useful to you please show your respect by rating their posts.
    3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
    4) Before posting your question, make sure you checked this links:
    MICROSOFT MSDN -- VB FORUMS SEARCH

    5)Support Classic VB - A PETITION TO MICROSOFT

    ___________________________________________________________________________________
    THINGS TO KNOW ABOUT VB: || VB Examples/Demos
    What are Classes?
    || -
    Where to place a sub/function?(global) || Webbrowser control

  5. #5

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    Its not an application where people can clik an enter a world. this application only shows how many people are online.
    you can use your own software. as long as it abides by jagex's rules. there are many world switchers out now. Just want my own personal one

  6. #6
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Runtime error '9': subscript out of range

    i think this happened because the variable named text2 is not an array and split function normally returns all the values distributed in an array
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  7. #7

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    What would i need to do to correct this?

    sheepy

  8. #8
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Runtime error '9': subscript out of range

    Quote Originally Posted by Sheepy_Daz
    What would i need to do to correct this?

    sheepy
    VB Code:
    1. If InStr(1, Text2.Text, "<") <> 0 Then
    2.     temp2 = Left(Split(Text2.Text, "World " & num & "<")(1), 30)
    3.     temp3 = InStr(1, temp2, "Players")
    4.     temp4 = InStr(1, temp2, "empty")
    5. Else
    6.     ...

  9. #9

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    thanks for you reply. i have tried what you have said an i get another compiler error. Next without for an in this part of the code.
    This part in red gets highlighted yellow, an the part in red down the bottom is what gets highlighted in blue
    VB Code:
    1. [COLOR=Red]Private Function worldplayerslist()[/COLOR]
    2. Dim worlds(117) As String, temp() As String, temp2 As String, temp3 As String, temp4 As String
    3. On Error GoTo skip2
    4. Text2.Text = Inet1.OpenURL("http://runescape.com/serverlist.ws?lores.x=396&lores.y=217&plugin=0&order=WMLP")
    5. DoEvents
    6. For num = 1 To 117
    7.     On Error GoTo Offline
    8.     If InStr(1, Text2.Text, "<") <> 0 Then
    9.     temp2 = Left(Split(Text2.Text, "World " & num & "<")(1), 30)
    10.     temp3 = InStr(1, temp2, "Players")
    11.     temp4 = InStr(1, temp2, "empty")
    12.     If (temp4 > 0) Then
    13.         temp2 = "OFFLINE"
    14.     Else
    15.         If (temp3 > 0) Then
    16.             temp2 = Split(Split(temp2, " Players")(0), "<td>")(1)
    17.         Else
    18.             temp2 = "Full"
    19.         End If
    20.     End If
    21.     Text1(num - 1).Text = temp2
    22.     GoTo skip
    23. Offline:
    24.     Text1(num - 1).Text = "Offline"
    25. skip:
    26. [COLOR=red]Next[/COLOR] num
    27. skip2:
    28. End Function

    Any ideas?

  10. #10
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Runtime error '9': subscript out of range

    You are missing the End If for the If Instr... line.

  11. #11

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    now im getting runtime error 13. type mismatch. the part in red is getting highlighted :-S Please excuse me as im fairly new to vb an dont no much about the error or runtime messages
    VB Code:
    1. Private Function worldplayerslist()
    2. Dim worlds(117) As String, temp() As String, temp2 As String, temp3 As String, temp4 As String
    3. On Error GoTo skip2
    4. Text2.Text = Inet1.OpenURL("http://runescape.com/serverlist.ws?lores.x=396&lores.y=217&plugin=0&order=WMLP")
    5. DoEvents
    6. For num = 1 To 117
    7.     On Error GoTo Offline
    8.     If InStr(1, Text2.Text, "<") <> 0 Then
    9.     End If
    10.     temp2 = Left(Split(Text2.Text, "World " & num & "<")(1), 30)
    11.     temp3 = InStr(1, temp2, "Players") = 0
    12.     temp4 = InStr(1, temp2, "empty") = 0
    13.  
    14.     [COLOR=Red]If (temp4 > 0) Then[/COLOR]
    15.         temp2 = "OFFLINE"
    16.     Else
    17.         If (temp3 > 0) Then
    18.             temp2 = Split(Split(temp2, " Players")(0), "<td>")(1)
    19.         Else
    20.             temp2 = "Full"
    21.         End If
    22.     End If
    23.     Text1(num - 1).Text = temp2
    24.     GoTo skip
    25. Offline:
    26.     Text1(num - 1).Text = "Offline"
    27. skip:
    28. Next num
    29. skip2:
    30. End Function

    Sheepy

  12. #12
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Runtime error '9': subscript out of range

    Couple of things going on here. First, you are testing string types against integer types. temp4 is declared as a string, and you are testing to see if it is > 0. This leads to the second thing. When you give temp4 a value in this line,
    VB Code:
    1. temp4 = InStr(1, temp2, "empty") = 0
    this is what happens. The InStr returns a number if it finds the search string, or 0 if it doesn't. Then, it is compared to see if it equals zero. So, it if is found, temp4 is set to false. If it is not found, temp4 is set to true. Problem is, since temp4 is a string, the boolean that you are returning gets cast to a string "True" or "False". Thus, the type mismatch.

    Check out the code below, it might not be exactly what you need, but it will compile clean and let you work from there. (I changed the error handling and the GoTos because they were driving me batty).
    VB Code:
    1. Private Function worldplayerslist()
    2.    
    3.     Dim worlds(117) As String, temp() As String, temp2 As String, temp3 As Long, temp4 As Long
    4.    
    5.     On Error GoTo ErrHand
    6.     Text2.Text = Inet1.OpenURL("http://runescape.com/serverlist.ws?lores.x=396&lores.y=217&plugin=0&order=WMLP")
    7.     DoEvents
    8.    
    9.     For num = 1 To 117
    10.         If InStr(1, Text2.Text, "<") <> 0 Then
    11.             temp2 = Left(Split(Text2.Text, "World " & num & "<")(1), 30)
    12.             temp3 = InStr(1, temp2, " Players")
    13.             temp4 = InStr(1, temp2, "empty")
    14.         Else
    15.             'Do whatever you want to do when there is no delimiter. IE:
    16.             temp2 = "Offline"
    17.         End If
    18.        
    19.         If (temp4 > 0) Then
    20.             temp2 = "OFFLINE"
    21.         Else
    22.             If (temp3 > 0) Then
    23.                 temp2 = Split(Split(temp2, " Players")(0), "<td>")(1)
    24.             Else
    25.                 temp2 = "Full"
    26.             End If
    27.         End If
    28.         Text1(num - 1).Text = temp2
    29.     Next num
    30.    
    31. ErrHand:
    32.     If Err.Number <> 0 Then
    33.         'Recover from your error.  Probably a bad example, but something like this:
    34.         temp2 = "Offline"
    35.         Resume Next
    36.     End If
    37.    
    38. End Function

  13. #13

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    The way you explained that was very helpful thanks. ill give this a try an let you know how i get on :-)

    Thanks
    Sheepy

  14. #14

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    ok compiles fine, still 1 problem tho :-S.
    all worlds after 116 are coming up as full, even tho there not full. if you get me?
    is there some way i can cut world 116 out of the code or get it not to check for world 116? as is doesnt get used for anything.

  15. #15
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Runtime error '9': subscript out of range

    You can try resetting your variable at the bottom of the loop, otherwise I'd have to see what the input text looked like.
    VB Code:
    1. Text1(num - 1).Text = temp2
    2.         temp2 = vbNullString    'Add these three lines.
    3.         temp3 = 0
    4.         temp4 = 0
    5.     Next num

  16. #16

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    No Joy with resetting the variables.
    I sort of know whats happening, just dunno how to fix it. what gets me is if the other worlds are full it dont mess it up at all.
    because ther is no world 116 it gets messed up
    Last edited by Sheepy_Daz; Mar 25th, 2006 at 12:18 PM.

  17. #17

    Thread Starter
    Member Sheepy_Daz's Avatar
    Join Date
    Mar 2006
    Posts
    39

    Re: Runtime error '9': subscript out of range

    Sorry to double post, but problem solved.
    Thanks to all who helped
    Sheepy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width