For Programmers: Free Programming Magazines  


Home > Archive > ASP > January 2006 > Re: Dynamic variables syntax









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Dynamic variables syntax
Bob Barrows [MVP]

2006-01-26, 7:55 am

Mr. Smith wrote:
> 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/e...dmthgetrows.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
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com