Help: Dynamic variables syntax

M

Mr. Smith

Hi.
What's the correct syntax here:
(given the recordset myRS with x records

do until myRS.EOF
txtvar & myRS("serial_number") = myRS("serial_name")
myRS.movenext
loop

..asp will not build the dynamic variables
txtvar1 = "Some name"
txtvar2 = "Any name"
txtvar3 = "Your name"
.....
Based on the serial_number in the current recordset

How can I builde the dynamic variables, and give them a value from my
recordset?

Hope some of you understand, and can help me out....

Regards
Mr. Smith
 
E

Evertjan.

Mr. Smith wrote on 26 jan 2006 in microsoft.public.inetserver.asp.general:
What's the correct syntax here:
(given the recordset myRS with x records

do until myRS.EOF
txtvar & myRS("serial_number") = myRS("serial_name")
myRS.movenext
loop

.asp will not build the dynamic variables
txtvar1 = "Some name"
txtvar2 = "Any name"
txtvar3 = "Your name"
....
Based on the serial_number in the current recordset

How can I builde the dynamic variables, and give them a value from my
recordset?

Hope some of you understand, and can help me out....

Use an array:

dim txtvar(100)

txtvar(+myRS("serial_number")) = ...
 
B

Bob Barrows [MVP]

Mr. Smith said:
Hi.
What's the correct syntax here:
(given the recordset myRS with x records

do until myRS.EOF
txtvar & myRS("serial_number") = myRS("serial_name")
myRS.movenext
loop

.asp will not build the dynamic variables
txtvar1 = "Some name"
txtvar2 = "Any name"
txtvar3 = "Your name"

I'm assuming the serial numbers do not have to start at 1, or be
consecutive? If so, that means an array will not suit your purposes.
Instead, use a Dictionary object.

Firstly, use GetRows
(http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp) to
move your entire recordset into an array. It is more efficient to loop
through an array than it is to loop through a recordset
(http://www.aspfaq.com/show.asp?id=2467). Optionally, you can even control
which fields get included in the array, like this

dim ar
'open your recordset, then
if not myRS.EOF then ar=myRS.GetRows(,,array("serial_name")

You can now immediately close and destroy your recordset and connection,
freeing up those resources and minimizing the time that you are connected to
the database, which is a good thing:

myRS.Close: Set myRS=nothing
myConn.Close: Set myConn=Nothing

Then check to see if ar is an array. If it is, create the Dictionary object:
If not, report that no records were returned.

dim dict
dim i
if not isarray(ar) then
response.write "no records"
else
set dict=createobject("scripting.dictionary")

Then loop through the array:

for i=0 to ubound(ar,2)
dict.add ar(0,i), ar(1,i)
next

Now, instead of saying

response.write "txtvar2 contains " & txtvar2

you can say

response.write "dict(""2"") contains " & dict("2")

end if

HTH,
Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top