Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: Putting recordset into array


Message #1 by "Chirag Shah" <chiragiit@y...> on Fri, 25 Oct 2002 17:51:18
Code help please!

I am trying to put recrodset into array. (putting various records return 
by recordset as elements of an array)

Giving me subsciprt out of range error on asterisk marked line

       Dim i
       i=0
       Dim myArray 
       While NOT rs.EOF
	  myArray(i) = CInt(rs("ID")) ****************
	  rs.MoveNext
	  i= i+1
	Wend

any help? Thanks in advance
Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Fri, 25 Oct 2002 12:59:03 -0400
A better approach would be to use the GetRows function.  More details on
that available here:

http://www.learnasp.com/learn/whygetrows.asp

Hope this helps. :)
Regards,
Peter


-----Original Message-----
From: Chirag Shah [mailto:chiragiit@y...]
Sent: Friday, October 25, 2002 5:51 PM
To: ASP Databases
Subject: [asp_databases] Putting recordset into array


Code help please!

I am trying to put recrodset into array. (putting various 
records return 
by recordset as elements of an array)

Giving me subsciprt out of range error on asterisk marked line

       Dim i
       i=0
       Dim myArray 
       While NOT rs.EOF
	  myArray(i) = CInt(rs("ID")) ****************
	  rs.MoveNext
	  i= i+1
	Wend

any help? Thanks in advance
Message #3 by "Chirag Shah" <chiragiit@y...> on Fri, 25 Oct 2002 18:16:13
Thanks Peter,
I think, does GetRows is more useful when you are dealing with multi 
dimensional array? There is a silly mistake in my code but I am not sure 
what?
Message #4 by "Peter Foti (PeterF)" <PeterF@S...> on Fri, 25 Oct 2002 13:36:00 -0400
Actually, using GetRows will still be faster because you are only making 1
call to the database, and you don't need to keep doing MoveNext through your
recordset.

In your code:

      Dim i
       i=0
       Dim myArray 
       While NOT rs.EOF
	  myArray(i) = CInt(rs("ID")) ****************
	  rs.MoveNext
	  i= i+1
	Wend

Your error is that you are declaring myArray as a variable, and not as an
array.  To declare it as a variable, you must know how many items it will
hold.  In this case, you would want the total number of rs("ID") values that
you intend to store.  This becomes inefficient because now you must count
all of the records in the recordset.  If you had this value, then your
updated code might look like this:

      Dim i
      Dim myArray(totalRecords)
      For i = 0 To totalRecords
         myArray(i) = CInt(rs("ID"))
         rs.MoveNext
      Next

But again, you would need to have the count in totalRecords, and GetRows is
a better approach.
Regards,
Peter




-----Original Message-----
From: Chirag Shah [mailto:chiragiit@y...]
Sent: Friday, October 25, 2002 6:16 PM
To: ASP Databases
Subject: [asp_databases] Re: Putting recordset into array


Thanks Peter,
I think, does GetRows is more useful when you are dealing with multi 
dimensional array? There is a silly mistake in my code but I am 
not sure 
what?
Message #5 by Mark Eckeard <meckeard2000@y...> on Fri, 25 Oct 2002 10:58:35 -0700 (PDT)
And to add to Peter's comments:

- Looping thru an array is faster than looping thru a
recordset.
- An array is less resource intensive than a
recordset.
- When declaring the array to hold the recordset, you
don't need to know the amount of records.  Just
dimension the variable like normal and it takes care
of iteself.

If you have any problems, post your code so we can
help.

Mark
--- "Peter Foti (PeterF)"
<PeterF@S...> wrote:
> Actually, using GetRows will still be faster because
> you are only making 1
> call to the database, and you don't need to keep
> doing MoveNext through your
> recordset.
> 
> In your code:
> 
>       Dim i
>        i=0
>        Dim myArray 
>        While NOT rs.EOF
> 	  myArray(i) = CInt(rs("ID")) ****************
> 	  rs.MoveNext
> 	  i= i+1
> 	Wend
> 
> Your error is that you are declaring myArray as a
> variable, and not as an
> array.  To declare it as a variable, you must know
> how many items it will
> hold.  In this case, you would want the total number
> of rs("ID") values that
> you intend to store.  This becomes inefficient
> because now you must count
> all of the records in the recordset.  If you had
> this value, then your
> updated code might look like this:
> 
>       Dim i
>       Dim myArray(totalRecords)
>       For i = 0 To totalRecords
>          myArray(i) = CInt(rs("ID"))
>          rs.MoveNext
>       Next
> 
> But again, you would need to have the count in
> totalRecords, and GetRows is
> a better approach.
> Regards,
> Peter
> 
> 
> 
> 
> -----Original Message-----
> From: Chirag Shah [mailto:chiragiit@y...]
> Sent: Friday, October 25, 2002 6:16 PM
> To: ASP Databases
> Subject: [asp_databases] Re: Putting recordset into
> array
> 
> 
> Thanks Peter,
> I think, does GetRows is more useful when you are
> dealing with multi 
> dimensional array? There is a silly mistake in my
> code but I am 
> not sure 
> what?
> 


__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/
Message #6 by "Ken Schaefer" <ken@a...> on Sat, 26 Oct 2002 23:26:19 +1000
Do it this way:

<%
Dim arrResults
'
If not objRS.EOF then
    arrResults = objRS.GetRows
End If
objRS.Close
Set objRS = Nothing
'
'
If isArray(arrResults) then
    ' We have records
Else
    ' We have no records
End If
%>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Chirag Shah" <chiragiit@y...>
Subject: [asp_databases] Putting recordset into array


: Code help please!
: 
: I am trying to put recrodset into array. (putting various records return 
: by recordset as elements of an array)
: 
: Giving me subsciprt out of range error on asterisk marked line
: 
:        Dim i
:        i=0
:        Dim myArray 
:        While NOT rs.EOF
:   myArray(i) = CInt(rs("ID")) ****************
:   rs.MoveNext
:   i= i+1
: Wend
: 
: any help? Thanks in advance


  Return to Index