Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subscript out of range when populating an array 2

Status
Not open for further replies.

brad1978

Programmer
Feb 13, 2009
30
CA
Hi all,
Another quick question. I'm trying to populate 2 arrays from my SQL query and I keep getting the "Subscript out of range" error. Can someone explain what I'm doing wrong?

Code:
dim counter, prevComments(), R_to()
RS.movefirst 
counter = 0
	do until RS.EOF
		R_to(counter) = RS("R_postby")
		prevComments(counter) = RS("R_comments")
		counter = counter + 1 
RS.movenext
loop

Cheers!
 
In VBScript you cannot change the size of an array on the fly, so you'll need to either set it's size when you first declare it, or redim it each time you itterate through your array.

Code:
dim counter, prevComments(), R_to()
RS.movefirst 
counter = 0
    do until RS.EOF
		[b]redim R_to(counter)[/b]
		R_to(counter) = RS("R_postby")
		[b]redim prevComments(counter)[/b]
		prevComments(counter) = RS("R_comments")
        counter = counter + 1 
RS.movenext
loop

depending on how you open your recordset, you may be able to get the recordcount using rs.recordcount, if not you'll need to do a quick loop to find the number of records:

Code:
' start counting records
numRecords = rs.recordcount 
' if that doesn't work, the do it like this:
rs.movefirst
dim counter
numRecords = 0
do until rs.eof
	numRecords = numRecords + 1
loop
' end counting records

dim prevComments(numRecords)
dim R_to(numRecords)

RS.movefirst 
counter = 0
    do until RS.EOF
        R_to(counter) = RS("R_postby")
        prevComments(counter) = RS("R_comments")
        counter = counter + 1 
RS.movenext
loop

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Use
Code:
ReDim [b][red]Preserve[/red][/b] prevComments(counter)
so you don't erase the contents of the array each time you ReDim it.

Lee
 
good call lee - forgot about that :)

--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
you can also do a select count(somefield) to get number of records.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top