Help: Dynamic variables syntax

Discussion in 'ASP General' started by Mr. Smith, Jan 26, 2006.

  1. Mr. Smith

    Mr. Smith Guest

    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
     
    Mr. Smith, Jan 26, 2006
    #1
    1. Advertising

  2. Mr. Smith

    Evertjan. Guest

    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")) = ...

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 26, 2006
    #2
    1. Advertising

  3. Re: Dynamic variables syntax

    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/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
    --
    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"
     
    Bob Barrows [MVP], Jan 26, 2006
    #3
  4. Mr. Smith

    Guest

    , Jan 29, 2006
    #4
  5. Re: Dynamic variables syntax

    wrote:
    > You may want to have a look at this:
    >
    > Using the Eval and Execute Functions
    > http://www.4guysfromrolla.com/webtech/030300-1.shtml


    And you may want to look at these to see why you shouldn't take that advice:
    http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx
    http://blogs.msdn.com/ericlippert/archive/2003/11/04/53335.aspx

    --
    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"
     
    Bob Barrows [MVP], Jan 30, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    426
    Scott Allen
    Aug 10, 2004
  2. Replies:
    9
    Views:
    972
  3. Dan
    Replies:
    3
    Views:
    163
  4. gabriele renzi
    Replies:
    2
    Views:
    214
    gabriele renzi
    Dec 31, 2005
  5. Ken Bloom
    Replies:
    3
    Views:
    220
Loading...

Share This Page