Valhalla Legends Forums Archive | Visual Basic Programming | VB: run time error 9: sub script out of range

AuthorMessageTime
Tontow
im stumped, please help.

after the msgbox at 3000 it stops and i get a "run time error 9: sub script out of range"

and yes its a long text file

[code]
Public Sub writethis(StrMsg As String)
Dim intFile As Integer
Dim StrNew As String
Dim StrBuffer As String
intFile = FreeFile
Open ("C:\WINDOWS\Desktop\Temperary\finished.txt") For Input As intFile
Do While Not EOF(intFile)
Line Input #intFile, StrBuffer
StrNew = StrNew & StrBuffer & vbCrLf
Loop
Close #intFile
StrNew = StrMsg & vbCrLf & StrNew
Open "C:\WINDOWS\Desktop\Temperary\finished.txt" For Output As #intFile
Print #intFile, StrNew
Close #intFile
End Sub
'------------------------------------------------------------------------
Private Sub Form_Load()
Dim myArray() As String, buffer As String
Open "C:\WINDOWS\Desktop\Temperary\base.txt" For Binary Access Read As #1
buffer = Space$(LOF(1))
Get #1, , buffer
myArray = Split(buffer, vbCrLf)


Dim frist As String
Dim second As String
Dim third As String
Dim total As String
Dim total2 As String

frist = "set udg_SpecialEffectNumbers["
second = "]='"
third = "'" & vbCrLf
Close

Dim index As Integer
For index = 0 To 3371
total = total & frist & index & second & myArray(index) & third
Select Case index
Case 1000
MsgBox (index)
Case 2000
MsgBox (index)
Case 3000
MsgBox (index)
End Select

Next index

writethis total
writethis total2

MsgBox ("done")
End Sub
[/code]
April 20, 2004, 4:32 PM
iago
I would imagine that "myArray(index)" is going off the end of the array (the subscript index is out of range of myArray).

<edit> Instead of looping 3371 times, isn't there a better way to do that in vb? Like, myArray.upperBound() or something? I can't remember vb that well, but I seem to recall something to get the upper bound of an array.

<edit2> also, shoudln't close have the reference number, like "close #1"?

<edit3> why are you opening the file for binary access? Binary doesn't guarentee that vbcrlf's are detected correctly.
April 20, 2004, 4:38 PM
LoRd
[quote author=iago link=board=31;threadid=6393;start=0#msg55946 date=1082479136]
<edit> Instead of looping 3371 times, isn't there a better way to do that in vb? Like, myArray.upperBound() or something? I can't remember vb that well, but I seem to recall something to get the upper bound of an array.
[/quote]

[code]
For I = 0 To UBound(myArray())
'Blah
Next I
[/code]
April 20, 2004, 5:35 PM
iago
Aha, UBound. Thanks :)

So you'll probably want to do
for i = 0 to UBound(myArray) -1
...

next i
April 20, 2004, 5:51 PM
Eli_1
No.
[code]
Dim Blah(15) as String
' // 16 elements in this array (0-15)
' // In C/C++ Blah[15] is 15 elements (0-14 -- 15 does not exist).
Blah(15) = "weee"
[/code]
is perfectly legal in VB, as compared to C/C++ where blah[15] would be a "fence post error."

So:
[code]
For i = 0 to UBound(Blah)
...
Next i
[/code]is correct.
April 20, 2004, 11:06 PM
Grok
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though. Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you. Just from looking at your existing code, it has to be way slow. This version will be much faster --

[code]Public Sub AppendMessage(strMsg As String)
Dim pfNum As Integer
Dim sFile As String
sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
pfNum = FreeFile
Open sFile For Append As #pfNum
Print #pfNum, strMsg
Close #pfNum
End Sub

Private Sub Form_Load()
Dim fNum As Integer
Dim msgLine As String
Dim s1 As String, s2 As String, s3 As String
s1 = "set udg_SpecialEffectNumbers["
s2 = "]='"
s3 = "'"
sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
pfNum = FreeFile
Open sFile For Input As #pfNum
Do While EOF(pfNum) = False
Line Input #pfNum, msgLine
msgLine = s1 & lineCount & S2 & msgLine & s3
AppendMessage msgLine
Loop
Close #pfNum
MsgBox "Done"
End Sub[/code]
April 20, 2004, 11:52 PM
Eibro
[quote author=Eli_1 link=board=31;threadid=6393;start=0#msg56002 date=1082502381]
No.
[code]
Dim Blah(15) as String
' // 16 elements in this array (0-15)
' // In C/C++ Blah[15] is 15 elements (0-14 -- 15 does not exist).
Blah(15) = "weee"
[/code]
is perfectly legal in VB, as compared to C/C++ where blah[15] would be a "fence post error."

So:
[code]
For i = 0 to UBound(Blah)
...
Next i
[/code]is correct.
[/quote]IIRC, Dim Blah(15) as String will create a string with 15 elements ( 1-15 ). Unless Option Base 0 is specified, in which case elements would be numbered ( 0-14 ), as in most other languages.
April 21, 2004, 4:34 AM
Stealth
It's the other way around -- Option Base 1 will start the array at 1, by default Array(15) will be 0 to 15. Additionally, you can specify Array(# to #) for a more specific size.

[quote]
So you'll probably want to do
for i = 0 to UBound(myArray) -1
...

next i[/quote]

The UBound function returns the proper upper boundary of the array, not the number of elements as it does elsewhere, so you don't have to loop to UBound - 1. :)
April 21, 2004, 5:20 AM
Eli_1
[quote author=Eibro link=board=31;threadid=6393;start=0#msg56060 date=1082522048] Unless Option Base 0 is specified, in which case elements would be numbered ( 0-14 ), as in most other languages.
[/quote]Option base 0 is default and would set from 0 to 15.
April 21, 2004, 10:56 AM
Tontow
[quote author=Grok link=board=31;threadid=6393;start=0#msg56009 date=1082505154]
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though. Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you. Just from looking at your existing code, it has to be way slow. This version will be much faster --

[code]Public Sub AppendMessage(strMsg As String)
Dim pfNum As Integer
Dim sFile As String
sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
pfNum = FreeFile
Open sFile For Append As #pfNum
Print #pfNum, strMsg
Close #pfNum
End Sub

Private Sub Form_Load()
Dim fNum As Integer
Dim msgLine As String
Dim s1 As String, s2 As String, s3 As String
s1 = "set udg_SpecialEffectNumbers["
s2 = "]='"
s3 = "'"
sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
pfNum = FreeFile
Open sFile For Input As #pfNum
Do While EOF(pfNum) = False
Line Input #pfNum, msgLine
msgLine = s1 & lineCount & S2 & msgLine & s3
AppendMessage msgLine
Loop
Close #pfNum
MsgBox "Done"
End Sub[/code]
[/quote]

thank you, thats much faster but its not putting in the number between the [], wich is kinda improtaint
April 22, 2004, 3:14 AM
Grok
[quote author=Tontow link=board=31;threadid=6393;start=0#msg56261 date=1082603647]
[quote author=Grok link=board=31;threadid=6393;start=0#msg56009 date=1082505154]
Holy Mackeral!!!

VB is not installed on this computer, but please let me rewrite it for you ... you'll have to syntax check it though. Now I left out the MsgBox at 1000, 2000, and 3000 linecount, since I figured those were just progress indicators for you. Just from looking at your existing code, it has to be way slow. This version will be much faster --

[code]Public Sub AppendMessage(strMsg As String)
Dim pfNum As Integer
Dim sFile As String
sFile = "C:\WINDOWS\Desktop\Temperary\finished.txt"
pfNum = FreeFile
Open sFile For Append As #pfNum
Print #pfNum, strMsg
Close #pfNum
End Sub

Private Sub Form_Load()
Dim fNum As Integer
Dim msgLine As String
Dim lineCount As Long
Dim s1 As String, s2 As String, s3 As String
s1 = "set udg_SpecialEffectNumbers["
s2 = "]='"
s3 = "'"
sFile = "C:\WINDOWS\Desktop\Temperary\base.txt"
pfNum = FreeFile
Open sFile For Input As #pfNum
lineCount = 0
Do While EOF(pfNum) = False
Line Input #pfNum, msgLine
lineCount = lineCount + 1
msgLine = s1 & lineCount & S2 & msgLine & s3
AppendMessage msgLine
Loop
Close #pfNum
MsgBox "Done"
End Sub[/code]
[/quote]

thank you, thats much faster but its not putting in the number between the [], wich is kinda improtaint
[/quote]

Fixed.
April 22, 2004, 3:45 AM
Tontow
thank you very much
April 22, 2004, 9:36 PM

Search