Link to home
Start Free TrialLog in
Avatar of MickeC
MickeCFlag for Sweden

asked on

Array from loop

Hi!
I have a simple problem with arrays.

I want to build an array of a loop, then i want to store the result in a variable.

I have a loop like this:

while not rsSpare.eof
response.write rsSpare("SupportProcessSparepart")&""
rsSpare.movenext
wend

And if there is more than one sparepart then i want to seperate them with an break and then i want to store this in a variable named spareparts and the output will be:

sparepart1
sparepart2
...

I will use the variable in an mailcomponent. Can you help me, i know that this is basic, but i am in a hurry and i am really bad at arrays :-)
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nap0leon
nap0leon

Here is how you create an array of N size, adding to it as you loop through your object.
In this example, I merely loop through the numbers 1 through 10.
<%
Dim arrSpareParts() 
Dim i 
i=0

While i < 10
	Redim Preserve arrSpareParts(i)
	arrSpareParts(i) = "Value_" & i
	i = i + 1
Wend

For Each item In arrSpareParts
	Response.Write(item & "<br />")
Next
%>

Open in new window


Using your rsSpare object, it would look like this:
<%

Dim arrSpareParts() 
Dim i 

i=0

while not rsSpare.eof
	Redim Preserve arrSpareParts(i)
	arrSpareParts(i) = rsSpare("SupportProcessSparepart")
	i = i + 1
	rsSpare.movenext
wend

For Each item In arrSpareParts
	Response.Write(item & "<br />")
Next
%>

Open in new window


A little bit cleaner, but I'm not sure if I have the syntax correct for rsSpare.Index, would be something like this (not sure if the first value is 0 or 1 - if it is 1, then you will want to subtract 1 from it so that the first item in your array is number 0:
<%

Dim arrSpareParts() 
Dim i 

while not rsSpare.eof
	i = rsSpare.Index
	Redim Preserve arrSpareParts(i)
	arrSpareParts(i) = rsSpare("SupportProcessSparepart")
	rsSpare.movenext
wend

For Each item In arrSpareParts
	Response.Write(item & "<br />")
Next
%>

Open in new window