Forum Moderators: open

Message Too Old, No Replies

Assign a variable

This is a stumper - is it possible?

         

dotme

8:58 pm on Jul 7, 2004 (gmt 0)

10+ Year Member



Sanity check - I may be attempting the impossible here :-)

I have about 50 fields in each record in a SQL table. I am building an ASP page with a form to update the values in the fields. I have done this a bazillion times the old fashioned way, pulling each field by name. Today I tried a shortcut. Code below.

For Each x In objRS.Fields
response.write x.Name & " - " & x & "<br>"
Next

Sure enough, the above code writes all 50 fieldnames and their values to screen. (x.Name is the field name, and x is the value)

Slick. But... I think I need to be able to assign a variable to a variable... so that the x.Name (which is the field name) becomes a variable, with x as the value. I can't seem to do that!

In the above example, let's say the first iteration through the loop produces

x.Name="FirstName"
x="Joe"

What I need to do is end up with a variable

FirstName = "Joe"

So later in the page, I can write out the variable FirstName and display the value "Joe"... and hopefully do the same for the other 49 fields in the database.

Any hints? Any clues? Is this impossible? lol

Thanks in advance for the help - even if it's not possible, at least knowing for sure will put me out of my misery!

JD

macrost

1:07 am on Jul 8, 2004 (gmt 0)

10+ Year Member



dotme,
I think that the only way you would be able to do that is if you use fso to write an asp file that will use the variables. AFAIK, you can't do what you want to do. :(

dotme

12:05 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



With the help of another board, I solved it! I'm posting the code here in case others are interested. Thanks to macrost and all others who took time to look into this for me.

JD

For Each x In objRS.Fields
varname = x.Name
val = chr(34) & x & chr(34)
Execute(varname &" = "& val)
Next

TheNige

8:57 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



you could have also loaded the name and values to a 2D array.

dotme

9:10 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Ah, well yes - A good suggestion, and cleaner than "Variable variables" which is what I believe this is called. I thought of that also... But if someone else gets into SQL and inserts a new field in the middle of the record, that throws off the numeric array values and I wanted to code this page only once and be done with it.

Although the ASP gathers only required data, it's possible that internally fields could be added and/or inserted by someone else and I figured an array would maybe need more ongoing maintenence.

Thanks again to all for feedback and help!

JD