473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

VB6: Subscript out of range

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
1 2655
Killer42
8,435 Expert 8TB
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

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

Similar topics

20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
3
by: Rani Ponmathi | last post by:
Please can I know how to display the formulae using superscript (Ex:- E = mc2 here 2 is superscript text) and subscript (H20 - here 2 is subscript text) in Label control of VB6 Thank You!
0
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
0
by: wennerd | last post by:
I am getting a Subscript out of range after executing the following command Set oRstPick = connTemp.Execute(sSQL) It is a simple query: sSQL = "SELECT idCategory, categoryDesc, idParentCategory...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
6
by: Andy | last post by:
Hi all, I started developing a little app on my Mac using XCode some month ago. The app is running fine on my mac like a sharm. Now I am nearly ready and yesterday I moved the whole source code...
4
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 ...
6
by: brandon01 | last post by:
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx... Private Sub getBday() bDay = List1.List(nextBday) bDayArr = Split(bDay, " - ") Text1.Text =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.