423,680 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,680 IT Pros & Developers. It's quick & easy.

VB6: Subscript out of range

P: 1
Hello, I am making a guessing game which enables you to guess by punching in the letters. Anyway, I was making making then and I encountered this error:

"Runtime-error 9Subscript out of range"

Expand|Select|Wrap|Line Numbers
  1. Dim question()
  2. Dim answer()
  3. Dim ans()
  4. Dim qno As Integer
  5. Dim letterClick As String
  6. Dim wrongCount As Integer
  7.  
  8.  
  9. Public Sub showQuestion()
  10. Dim letter
  11. Dim X As Integer
  12. Dim Y As Integer
  13.  
  14.  
  15. Randomize
  16. question = Array("A programming language originally developed by James Gosling at Sun Microsystems", _
  17.             "A programming language from Microsoft", _
  18.             "Determines when the user wants to do something such as exit the application or begin printing")
  19.  
  20.  
  21. answer = Array("Java", _
  22.         "Visual_Basic", _
  23.         "Command_Button")
  24.  
  25. qno = Int(Rnd * (UBound(question) + 1))
  26. ReDim ans(Len(answer(qno)), 2)
  27.  
  28. lblquestion.Caption = question(qno)
  29.  
  30. For X = 0 To Len(answer(qno)) - 1
  31.     ans(X, 1) = Mid$(answer(qno), X + 1, 1)
  32.  
  33. Next X
  34.  
  35. For Y = 0 To Len(answer(qno)) - 1
  36.     If ans(Y, 1) = "_" Then
  37.         ans(Y, 2) = Chr$(32)
  38.     Else
  39.         ans(Y, 2) = "*"
  40.     End If
  41. Next Y
  42.  
  43. loadHint
  44. End Sub
And here is where the problem starts. It seems there was a mismatch with the value of x, which I can't figure out.
Expand|Select|Wrap|Line Numbers
  1. Private Sub loadHint()
  2. Dim X As Integer
  3. X = 0
  4. lblHint.Caption = ""
  5. For X = LBound(ans) To (UBound(ans) - 1)
  6.    lblHint.Caption = lblHint.Caption & ans(X, 2)
  7. Next X
  8. End Sub
  9. Private Sub checkLetters()
  10. Dim Y As Integer
  11. Dim X As Integer
  12.  
  13. Y = 0
  14. For Y = LBound(ans) To UBound(ans)
  15.     If UCase(ans(Y, 1)) = UCase(letterClick) Then
  16.         ans(Y, 2) = ans(Y, 1)
  17.         loadHint
  18.         End If
  19.     If answer(qno) = lblHint.Caption Then
  20.         Me.Hide
  21.         frmWinner.Show vbModal
  22.     End If
  23.  
  24.     If Y = UBound(ans) Then
  25.         wrongCount = wrongCount + 1
  26.  
  27. End If
  28.  
  29. Next Y
  30. End Sub
  31. Private Sub cmdA_Click()
  32. letterClick = "a"
  33. checkLetters
  34. playSound
  35. cmdA.Enabled = False
  36. End Sub
  37.  
  38. Private Sub cmdB_Click()
  39. letterClick = "b"
  40. checkLetters
  41. playSound
  42. cmdV.Enabled = False
  43. End Sub
  44.  
  45. Private Sub cmdC_Click()
  46. letterClick = "c"
  47. checkLetters
  48. playSound
  49. cmdC.Enabled = False
  50. End Sub
  51. Private Sub cmdD_Click()
  52. letterClick = "d"
  53. checkLetters
  54. playSound
  55. cmdD.Enabled = False
  56. End Sub
  57. Private Sub cmdE_Click()
  58. letterClick = "e"
  59. checkLetters
  60. playSound
  61. cmdE.Enabled = False
  62. End Sub
  63. Private Sub cmdF_Click()
  64. letterClick = "f"
  65. checkLetters
  66. playSound
  67. cmdF.Enabled = False
  68. End Sub
  69. Private Sub cmdG_Click()
  70. letterClick = "g"
  71. checkLetters
  72. playSound
  73. cmdG.Enabled = False
  74. End Sub
  75. Private Sub cmdH_Click()
  76. letterClick = "h"
  77. checkLetters
  78. playSound
  79. cmdH.Enabled = False
  80. End Sub
  81. Private Sub cmdI_Click()
  82. letterClick = "i"
  83. checkLetters
  84. playSound
  85. cmdI.Enabled = False
  86. End Sub
  87. Private Sub cmdJ_Click()
  88. letterClick = "j"
  89. checkLetters
  90. playSound
  91. cmdJ.Enabled = False
  92. End Sub
  93. Private Sub cmdK_Click()
  94. letterClick = "k"
  95. checkLetters
  96. playSound
  97. cmdK.Enabled = False
  98. End Sub
  99. Private Sub cmdL_Click()
  100. letterClick = "l"
  101. checkLetters
  102. playSound
  103. cmdL.Enabled = False
  104. End Sub
  105. Private Sub cmdM_Click()
  106. letterClick = "m"
  107. checkLetters
  108. playSound
  109. cmdV.Enabled = False
  110. End Sub
  111. Private Sub cmdN_Click()
  112. letterClick = "n"
  113. checkLetters
  114. playSound
  115. cmdN.Enabled = False
  116. End Sub
  117. Private Sub cmdO_Click()
  118. letterClick = "o"
  119. checkLetters
  120. playSound
  121. cmdO.Enabled = False
  122. End Sub
  123. Private Sub cmdP_Click()
  124. letterClick = "p"
  125. checkLetters
  126. playSound
  127. cmdP.Enabled = False
  128. End Sub
  129. Private Sub cmdQ_Click()
  130. letterClick = "q"
  131. checkLetters
  132. playSound
  133. cmdQ.Enabled = False
  134. End Sub
  135. Private Sub cmdR_Click()
  136. letterClick = "r"
  137. checkLetters
  138. playSound
  139. cmdR.Enabled = False
  140. End Sub
  141. Private Sub cmdS_Click()
  142. letterClick = "s"
  143. checkLetters
  144. playSound
  145. cmdS.Enabled = False
  146. End Sub
  147. Private Sub cmdT_Click()
  148. letterClick = "t"
  149. checkLetters
  150. playSound
  151. cmdT.Enabled = False
  152. End Sub
  153. Private Sub cmdU_Click()
  154. letterClick = "u"
  155. checkLetters
  156. playSound
  157. cmdU.Enabled = False
  158. End Sub
  159. Private Sub cmdV_Click()
  160. letterClick = "v"
  161. checkLetters
  162. playSound
  163. cmdV.Enabled = False
  164. End Sub
  165. Private Sub cmdW_Click()
  166. letterClick = "w"
  167. checkLetters
  168. playSound
  169. cmdW.Enabled = False
  170. End Sub
  171. Private Sub cmdX_Click()
  172. letterClick = "x"
  173. checkLetters
  174. playSound
  175. cmdX.Enabled = False
  176. End Sub
  177. Private Sub cmdY_Click()
  178. letterClick = "y"
  179. checkLetters
  180. playSound
  181. cmdY.Enabled = False
  182. End Sub
  183. Private Sub cmdZ_Click()
  184. letterClick = "z"
  185. checkLetters
  186. playSound
  187. cmdZ.Enabled = False
  188. End Sub
  189. Private Sub Form_Activate()
  190. enableButton
  191.  
  192. End Sub
  193.  
  194. Private Sub Form_Load()
  195. frmMenu.Show vbModal
  196. End Sub
  197. Private Sub playSound()
  198. SoundName$ = "c:\windows\tada.wav"
  199.    wFlags% = SND_ASYNC Or SND_NODEFAULT
  200.    X% = sndPlaySound(SoundName$, wFlags%)
  201. End Sub
  202.  
  203. Public Sub enableButton()
  204. cmdA.Enabled = True
  205. cmdB.Enabled = True
  206. cmdC.Enabled = True
  207. cmdD.Enabled = True
  208. cmdE.Enabled = True
  209. cmdF.Enabled = True
  210. cmdG.Enabled = True
  211. cmdH.Enabled = True
  212. cmdI.Enabled = True
  213. cmdJ.Enabled = True
  214. cmdK.Enabled = True
  215. cmdL.Enabled = True
  216. cmdM.Enabled = True
  217. cmdN.Enabled = True
  218. cmdO.Enabled = True
  219. cmdP.Enabled = True
  220. cmdQ.Enabled = True
  221. cmdR.Enabled = True
  222. cmdS.Enabled = True
  223. cmdT.Enabled = True
  224. cmdU.Enabled = True
  225. cmdV.Enabled = True
  226. cmdW.Enabled = True
  227. cmdX.Enabled = True
  228. cmdY.Enabled = True
  229. cmdZ.Enabled = True
  230. End Sub
  231.  
Thanks in advance.
Oct 4 '13 #1
Share this Question
Share on Google+
1 Reply


Expert 5K+
P: 8,435
Which line produces the error?

I would like to make a couple of minor points, just in case they're helpful:
  • Mid$ begins with position 1, not 0. This might lead to an error at some point.
  • The For loop sets the counter variable to the specified starting value, so you don't need to set it to 0 before the loop. There's no problem with setting it to a value first, it's just pointless.
  • If you want your program to produce a different sequence of random numbers each time it's run, include the statement Randomize some time during your startup process. It's actually a psedu-random number generator, which works from a "seed" number. If you don't change the seed it will produce the same series of numbers each time. Note that this can be very useful for testing, but is likely to be a pain in the real world.
    By default, I think Randomize uses the current time as the seed.
    Edit: Oops! I see you have a Randomize. Sorry. But I'll leave this here as general advice for anyone who bumps into this post in the future.
  • Ok, this one is a very trivial matter, but for reasons of general performance/efficiency, I recommend using Long format rather than Integer. Assuming we're talking about VB6, on a 32 bit processor, the 32-bit Long data type is the "native" size and doesn't require translation every time it's accessed.
  • I love the way you've made a lot of your code work with the actual size of the ans array (Lbound, Ubound) rather than assuming a particular size. This sort of flexible coding can make you life much easier when things change later.
Oct 4 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.