Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get field value with a variable reference 2

Status
Not open for further replies.

crabgrass

Technical User
Aug 29, 2007
111
US
Suppose you wanted to obtain a value from a recordset like this:

x = oRS("Local_2008")

but you wanted the year (2008) to be established by a variable, something like:

x = oRS("Local_" & myyear & ")"

How could you do this? I have tried several variations using "evaluate" but I can't seem to get the syntax right.
Thanks
 
Actually... when specifying a column name from a recordset object... you can.

Try this...

[tt][blue]x = oRS("Local_" & myyear)[/blue][/tt]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Hmm. I thought that answer I pointed at said you couldn't.

Learn every day, I guess.
 
Let me try to explain this....

You cannot concatenate variable NAMES, but you can concatenate variable values.

Ex:

Var1 = "Red"
Var2 = "Blue"

In this case Var1 and Var2 are variable names. You cannot...
Response.Write Var & "1"

The previous line will not print "Red". In fact, it will print "1". Since Var (as a variable) doesn't exist, ASP will create it, but it will be an empty string. So... empty string concatenate "1" will result in "1".

In this particular case, crabgrass is attempting to pass a string to the recordset function to return a value from a database column. The recordset object allow you to pass a string that represents the column name. Since it is a string parameter, you can concatenate values to get the column name you want.

crabgrass's only mistake was trying to include the close parenthesis in to the value that he was passing to the recordset function.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George. As is often the case, it was much easier than I expected.

- Don
 
Actually you CAN concatenate variable names... using the VBScript Eval() function.

Referring to the thread that BigRed1212 referred to (thread333-1463681), this would have worked;

Code:
bgcolor1 = "#0066CC"
bgcolor2 = "#009933"
bgcolor3 = "#FF0066"
bgcolor4 = "#FFCC00"
txtbg = "bgcolor"
rcount = 1
response.write "<font color='"&[COLOR=red]Eval([/color]txtbg&rcount[COLOR=red])[/color]&"'>TESTING</font>"
 
I was more pointing out an alternate than recommending it over George's superior solution (in both threads)... Didn't realize all that was going on behind the scenes with that function. Thanks for the link.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top