Dynamic Variables

S

scott

Is there a way to create dynamic variables when looping through a recordset?
For example below, after the 1st loop I'd have myVarA1 and myVarB1, after
2nd loop, I'd get myVarA2 and myVarB2.

CODE ***********************************

set objRS = GetMyRecordSet()
i=1
objRS.MoveFirst

Do While Not objRS.EOF

"myVarA" & i = objRS(0)
"myVarB" & i = objRS(1)

i = i + 1

objRS.MoveNext()
Loop
 
B

Bob Barrows [MVP]

scott said:
Is there a way to create dynamic variables

There is (the vbscript Execute statement), but it's not recommended. Use an
array.
when looping through a
recordset? For example below, after the 1st loop I'd have myVarA1 and
myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2.

With a recordset, a GetRows array seems ideal for your purpose

Dim arData
if not objRS.EOF then arData=objRS.GetRows(,,Array(0,1))
objRS.Close: Set objRS = Nothing

VarA1 would correspond to arData(0,0)
VarB1 would correspond to arData(1,0)
VarA2 would correspond to arData(0,1)
VarB2 would correspond to arData(1,1)
 
S

scott

Thanks, but I still have a problem. How can create say VarA1, VarA2, VarA3
dynamically without hardcoding them? I researched and found a redim method,
but was confused.
 
B

Bob Barrows [MVP]

You don't: use the array instead of the dynamic variables.

Instead of VarA1, use arData(0,0).
Instead of VarB2, use arData(1,1)

Bob Barrows
 
K

Kyle Peterson

ya, and I can see plenty of reasons why the guy wants variables with actual
names and doesn't want to keep track of all of them by their array

get your head out of your ass bob
 
B

Bob Barrows [MVP]

Kyle said:
ya, and I can see plenty of reasons why the guy wants variables with
actual names and doesn't want to keep track of all of them by their
array

You can??? Frankly, I can't think of a single reason for needing to do that.
Not once in my 20+ years of programming have I ever had a need to
dynamically create variables at runtime. You have??? This is so wrong-headed
on so many levels that I just don't know where to begin.

There are legitimate uses for Execute. This does not happen to be one of
them.

You can have the last word.
 
B

Bob Barrows [MVP]

PS. You can use constants to make the code a little easier to read and
maintain. For example

const A=0
const B=1

instead of VarA1, use arData(A,0)
instead of VarB2, use arData(B,1)
 
S

Scott

My reason for wanting the variables with actual names is that I have a lot
of them and it is just easier to have them defined before inserting them
into the html part of my asp report. All of them need specific formatting
like percents, decimal places, etc.

I'll just mix them altogether jus to get it done though. Thanks for all of
the info.
 
B

Bob Barrows [MVP]

Given that you still think you need hundreds of dynamic variables for this
task, it's obvious that I've done a poor job of conveying my advice. There
is something you are not understanding about the advice I gave. I guarantee
you that you do not need to have hundreds of variables for this task. An
array in combination with constants will allow you to accomplish this task
without resorting to the kludge of creating dynamic variables. If you can
provide a few more details about your task (hopefully a subset of that
task), I can provide you with the specific code that can be used to
accomplish it.

Contrary to Kyle's first post in this thread, performance is not the only
objection I have to using Execute in this manner (although it is a large
part of my objection that should not be trivialized). Maintainability and
resource usage also contribute to this bias. "Execute" has its place, but
I've never come across a need to use it.

Bob Barrows
 
K

Kyle Peterson

The guy never said anything about hundreds of variables.
It is probably a report that gets run once or twice a day if that.

It doesn't sound like he is building the next Ebay.

Give it up.
 
M

Michael D. Kersey

scott said:
Is there a way to create dynamic variables when looping through a recordset?
For example below, after the 1st loop I'd have myVarA1 and myVarB1, after
2nd loop, I'd get myVarA2 and myVarB2.

CODE ***********************************

set objRS = GetMyRecordSet()
i=1
objRS.MoveFirst

Do While Not objRS.EOF

"myVarA" & i = objRS(0)
"myVarB" & i = objRS(1)
You can do this with Session or Application variables:
Session("myVarA" & i ) = objRS(0)
Session("myVarB" & i )= objRS(1)

The syntax is slightly different but the effect will be the same.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top