Classic ASP Getting RS into an Array help

Associate
Joined
21 Sep 2008
Posts
1,593
Hi, I need help infomation from a record set into an 2D array.

Code:
<% set rs = conn.execute("SELECT * FROM table") %>
<%
Dim MyArray()
Redim MyArray(MaxRecords,1) 'Note i already have MaxRecords from previous code
i=0
While NOT rs.EOF
If rs.Fields.Item("Name") = "test" Then
'Need code to add 1 into an array for i.
Else
'Need code to add 0 into an array for i.
End IF

i=i+1
rs.MoveNext
Wend
%>

Tried a few array tutorials but they just confuse me more lol. I need to put a number into the array every time there is a 'hit' with the name, but i cant get the array syntax quite right. Also how do i output this into a string ? (so i can do other things with it later on.)
 
Last time i was adding stuff to an array using asp i used the dictionary object. Makes it easy to check for duplicates, add items, remove items etc. E.g to add an item:

set cart = createobject("scripting.dictionary")
cart.add someID, someItem

the someItem var could even be another array.

Just found another way (probably the way you were thinking of) using .GetRows(). See http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=161

I believe you can add an item to the array using redim iirc.
 
Last edited:
I'd do this by modifying the SQL query to include a field which is calculated to give you your 1 if necessary, then use getrows() to populate the array, something like...

Code:
set rsTable = server.createObject("ADODB.Recordset")
SQL = "SELECT Name, Iif(Name="Test",1,0) AS ArrayVal FROM Table;"
' /\ assuming you're using access - you'll probably need to modify the Iif if using something else
rsTable.open SQL, cn, 3, 1
	if not rsTable.eof then
		arrTable = rsTable.getRows()
	end if
rsTable.close
set rsTable = nothing

for i = 0 to ubound(arrTable, 2)
	' loop through your array with the numbers already in it!
next

*edit*
Here's some more on getRows - it's handy to know about as looping through arrays is a lot quicker than writing each field from a recordset.
 
Last edited:
Back
Top Bottom