473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic Variable Names - Syntax?

How does one refer to a variable name using a variable within a variable
name??? For example, I want to perform a SQL query and use the results of
the query to determine which variable to assign a value to. Let's say I've
dimmed 12 variables based on the months called "featured_x_product" where x
is the month. I'd like to refer to the variable with something
like: featured_" & R("month") & "_product where R("month") is the from the
current row in the db. I don't know the correct syntax or method for doing
so. A full example is provided below. Thanks in advance for you help!

dim featured_jan_product
dim featured_feb_product
dim featured_mar_product
dim featured_apr_product
....

SELECT status, month from products where year='2004' AND
page='featured_products'

While not R.EOF
If R("status") = "sold" Then
featured_" & R("month") & "_product = "Sold Out!" // (choose the
variable based on the "month" value in the current row)
Else
featured_" & R("month") & "_product = "Buy Now!"
End If
R.MoveNext
Wend


Jul 19 '05 #1
5 9994
Take a look at eval and exec in the VBScript documentation.

"Ken Halley" <ke*@alpacanation.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
How does one refer to a variable name using a variable within a variable
name??? For example, I want to perform a SQL query and use the results of
the query to determine which variable to assign a value to. Let's say I've dimmed 12 variables based on the months called "featured_x_product" where x is the month. I'd like to refer to the variable with something
like: featured_" & R("month") & "_product where R("month") is the from the
current row in the db. I don't know the correct syntax or method for doing so. A full example is provided below. Thanks in advance for you help!

dim featured_jan_product
dim featured_feb_product
dim featured_mar_product
dim featured_apr_product
...

SELECT status, month from products where year='2004' AND
page='featured_products'

While not R.EOF
If R("status") = "sold" Then
featured_" & R("month") & "_product = "Sold Out!" // (choose the
variable based on the "month" value in the current row)
Else
featured_" & R("month") & "_product = "Buy Now!"
End If
R.MoveNext
Wend

Jul 19 '05 #2
You can use the Eval function to do this, but I suggest not using Eval. It
makes for the most confusing and sloppy code you'll ever see.

How about using an array?

Dim featured_products(11) ''vbscrip arrays are zero based

If your storing your months in your database as "jan", "feb", "mar", and so
on, you'll have to deal with them with a select Case or some other thing.
Or, use a dictionary object or something. Let's try with a dictionary
object first.

Dim oDict
oDict.Add "jan", 0
oDict.Add "feb", 1
oDict.Add "mar", 2
oDict.Add "apr", 3
oDict.Add "may", 4
oDict.Add "jun", 5
oDict.Add "jul", 6
oDict.Add "aug", 7
oDict.Add "sep", 8
oDict.Add "oct", 9
oDict.Add "nov", 10
oDict.Add "dec", 11
While Not r.EOF
If r("status") = "sold" Then
featured_products(oDict(r("month"))) = "Sold out"
Else
''''etc.
My mind has been sleeping all week, so if this makes no sense or if I make
no sense, accept my apologies.

Ray at work

"Ken Halley" <ke*@alpacanation.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
How does one refer to a variable name using a variable within a variable
name??? For example, I want to perform a SQL query and use the results of
the query to determine which variable to assign a value to. Let's say I've dimmed 12 variables based on the months called "featured_x_product" where x is the month. I'd like to refer to the variable with something
like: featured_" & R("month") & "_product where R("month") is the from the
current row in the db. I don't know the correct syntax or method for doing so. A full example is provided below. Thanks in advance for you help!

dim featured_jan_product
dim featured_feb_product
dim featured_mar_product
dim featured_apr_product
...

SELECT status, month from products where year='2004' AND
page='featured_products'

While not R.EOF
If R("status") = "sold" Then
featured_" & R("month") & "_product = "Sold Out!" // (choose the
variable based on the "month" value in the current row)
Else
featured_" & R("month") & "_product = "Buy Now!"
End If
R.MoveNext
Wend

Jul 19 '05 #3
Thanks Ray. The situation is a little more complicated than my example in
that I have two pieces of the variable name that I want to make dynamic, the
month and the category (see below). So the single array idea doesn't solve
it. Any other thoughts?

The variable format is: category_month_color

R("category") & "_" & R("month") & "_color = "#0000FF"

"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:Oj**************@TK2MSFTNGP09.phx.gbl...
You can use the Eval function to do this, but I suggest not using Eval. It makes for the most confusing and sloppy code you'll ever see.

How about using an array?

Dim featured_products(11) ''vbscrip arrays are zero based

If your storing your months in your database as "jan", "feb", "mar", and so on, you'll have to deal with them with a select Case or some other thing.
Or, use a dictionary object or something. Let's try with a dictionary
object first.

Dim oDict
oDict.Add "jan", 0
oDict.Add "feb", 1
oDict.Add "mar", 2
oDict.Add "apr", 3
oDict.Add "may", 4
oDict.Add "jun", 5
oDict.Add "jul", 6
oDict.Add "aug", 7
oDict.Add "sep", 8
oDict.Add "oct", 9
oDict.Add "nov", 10
oDict.Add "dec", 11
While Not r.EOF
If r("status") = "sold" Then
featured_products(oDict(r("month"))) = "Sold out"
Else
''''etc.
My mind has been sleeping all week, so if this makes no sense or if I make
no sense, accept my apologies.

Ray at work

"Ken Halley" <ke*@alpacanation.com> wrote in message
news:OR**************@TK2MSFTNGP10.phx.gbl...
How does one refer to a variable name using a variable within a variable
name??? For example, I want to perform a SQL query and use the results of the query to determine which variable to assign a value to. Let's say I've
dimmed 12 variables based on the months called "featured_x_product" where x
is the month. I'd like to refer to the variable with something
like: featured_" & R("month") & "_product where R("month") is the from

the current row in the db. I don't know the correct syntax or method for

doing
so. A full example is provided below. Thanks in advance for you help!

dim featured_jan_product
dim featured_feb_product
dim featured_mar_product
dim featured_apr_product
...

SELECT status, month from products where year='2004' AND
page='featured_products'

While not R.EOF
If R("status") = "sold" Then
featured_" & R("month") & "_product = "Sold Out!" // (choose the variable based on the "month" value in the current row)
Else
featured_" & R("month") & "_product = "Buy Now!"
End If
R.MoveNext
Wend


Jul 19 '05 #4
> month and the category (see below). So the single array idea doesn't
solve
it. Any other thoughts?


Uh, a two dimensional array?

dim arrayName(<<<?>>>, 2)
arrayName(0, 0) = R("category")
arrayName(0, 1) = R("month")
arrayName(0, 2) = "#0000FF"

Dictionary objects can also hold complex types like arrays or even other
dictionary objects. I really think others are on the right track when they
suggest staying away from eval/exec... I just offered it as a possible
alternative.
Jul 19 '05 #5
I strongly suggest you find another approach that involves arrays or
something else. If you write a page that is laden with Evals and Execs,
it's going to be impossible to understand a week later when you look at it.
This is just my opinion.

Ray at home

"Ken Halley" <ke*@alpacanation.com> wrote in message
news:OP*************@TK2MSFTNGP10.phx.gbl...
Thanks Ray. The situation is a little more complicated than my example in
that I have two pieces of the variable name that I want to make dynamic, the month and the category (see below). So the single array idea doesn't solve it. Any other thoughts?

The variable format is: category_month_color

R("category") & "_" & R("month") & "_color = "#0000FF"

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bob Alston | last post by:
I am using a combo box to select the key to records and then go to the selected record on my form. Works well. However, the list, which contains people names, is rather long. What I would like...
9
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
7
by: ben | last post by:
hello, an algorithm book i'm reading talks about the connectivity problem/algorithm. it gives a number of examples where the connectivity problem applies to real life situations (like, the...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
3
by: simon | last post by:
hello, i have a form where there are multiple dropdown lists that will all be populated from the same initial data query. i have a vb class defined to make the stored proc call and that returns a...
3
by: Dan | last post by:
Is it possible to dynamically create and populate properties? Below is my class. I would like to define properties for each of the items in my two arrays without actually defining each one. (There...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
7
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.