View Full Version : Subscript out of range: 'i'
Boxhead
01-16-2004, 03:39 PM
I am getting the following error:
Subscript out of range: 'i' on the following script:
Do While NOT Recordset1.EOF
linevalue = "" 'reset linevalue to empty
For Each Item in Recordset1.fields
If (qsArray(i) = "export") Then
linevalue = linevalue & """" & Replace(Item.Value,"""","") & ""","
End If
i=i+1
Next
linevalue = Left(linevalue,Len(linevalue)-1)
f.WriteLine(linevalue)
Recordset1.MoveNext
Loop
Any ideas why?
monkey
Roy Sinclair
01-16-2004, 03:46 PM
You don't show any code setting an initial value for "i". It will start with a value of "undefined" which makes for a bad subscript.
Boxhead
01-16-2004, 04:18 PM
Sorry - missed the i=0 on my post, so this isnt the prob
monkey
Roy Sinclair
01-16-2004, 04:57 PM
What do you mean it isn't the problem? Where in your code does it go?
If it's before the posted code, how huge is the array in qsArray? Is it large enough to cover every field for every record in the database? Or do you not have the i=0 at the right place in the code to reset it between records from the database?
Boxhead
01-16-2004, 05:12 PM
Sorry my bad again! Lets start again, the code is as follows:
i=0
Recordset1.MoveFirst
Do While NOT Recordset1.EOF
linevalue = "" 'reset linevalue to empty
For Each Item in Recordset1.fields
If (qsArray(i) = "export") Then
linevalue = linevalue & """" & Replace(Item.Value,"""","") & ""","
End If
i=i+1
Next
linevalue = Left(linevalue,Len(linevalue)-1)
f.WriteLine(linevalue)
Recordset1.MoveNext
Loop
There are 30 fields in the database and 30 elements in the array. i have tried increasing the amout of elements to make suer and still the same prob.
Roy Sinclair
01-16-2004, 06:21 PM
And how many records in the recordset?
i=0
Recordset1.MoveFirst
Do While NOT Recordset1.EOF
linevalue = "" 'reset linevalue to empty
For Each Item in Recordset1.fields
If (qsArray(i) = "export") Then
linevalue = linevalue & """" & Replace(Item.Value,"""","") & ""","
End If
i=i+1
Next
linevalue = Left(linevalue,Len(linevalue)-1)
f.WriteLine(linevalue)
Recordset1.MoveNext
Loop
See the loop highlighted in red? Note that the SECOND record from the record set is going to start with a subscript of 30 and run until 59, the THIRD record will start at 60 and go to 89...
If you'd use indentation in your coding (which you may be doing but...) you'll be able to visually see the loops and bugs like this would be more readily apparent:
i=0
Recordset1.MoveFirst
Do While NOT Recordset1.EOF
linevalue = "" 'reset linevalue to empty
For Each Item in Recordset1.fields
If (qsArray(i) = "export") Then
linevalue = linevalue & """" & Replace(Item.Value,"""","") & ""","
End If
i=i+1
Next
linevalue = Left(linevalue,Len(linevalue)-1)
f.WriteLine(linevalue)
Recordset1.MoveNext
Loop
M@rco
01-19-2004, 12:53 PM
I suspect that you should be resetting i back to 0 within the Do...Loop (i.e. before the For line), since I can't think of a good reason why you'd be processing the records in this way otherwise.
;)
Am I right?!
Boxhead
01-19-2004, 02:23 PM
M@rco
Nail, head, hit!!!!
Sorted it after working out what Roy was talking about!! Cheer for the help both of you.
monkey
M@rco
01-19-2004, 03:14 PM
So what have we won? :p :D
Boxhead
01-19-2004, 03:22 PM
The respect and admiration of a fellow newsgrouper - in other words - nothing!!:p
Cheers again
monkey
M@rco
01-19-2004, 03:33 PM
lol! (That'll do for me) ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.