473,326 Members | 2,192 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,326 software developers and data experts.

Dynamic Variables

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
Dec 16 '05 #1
12 6191
scott wrote:
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)
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Dec 16 '05 #2
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.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
scott wrote:
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)
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Dec 16 '05 #3
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
scott wrote:
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.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
scott wrote:
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)
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get
a quicker response by posting to the newsgroup.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Dec 16 '05 #4
just use execute
do a google on it with asp

here are some I found real quick
http://www.aspdev.org/articles/asp-eval-execute/
http://www.4guysfromrolla.com/webtech/030300-1.shtml

Hey, if your have to you have to.

Some people are on here get all caught up in performance instead of just
telling people how to do things and leaving it at that


"scott" <sb*****@mileslumber.com> wrote in message
news:ue*************@TK2MSFTNGP12.phx.gbl...
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.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
scott wrote:
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)
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


Dec 16 '05 #5
Kyle Peterson wrote:
just use execute
do a google on it with asp

here are some I found real quick
http://www.aspdev.org/articles/asp-eval-execute/
http://www.4guysfromrolla.com/webtech/030300-1.shtml

Hey, if your have to you have to.


Please. You never "have-to" do crazy things. There's always a right way to
do them.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Dec 16 '05 #6
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
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uP*************@TK2MSFTNGP09.phx.gbl...
Kyle Peterson wrote:
just use execute
do a google on it with asp

here are some I found real quick
http://www.aspdev.org/articles/asp-eval-execute/
http://www.4guysfromrolla.com/webtech/030300-1.shtml

Hey, if your have to you have to.


Please. You never "have-to" do crazy things. There's always a right way to
do them.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Dec 16 '05 #7
Kyle Peterson wrote:
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Dec 16 '05 #8
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)

Bob Barrows [MVP] wrote:
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
scott wrote:
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.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
scott wrote:
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)
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get
a quicker response by posting to the newsgroup.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Dec 16 '05 #9
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.
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Kyle Peterson wrote:
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Dec 16 '05 #10
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

Scott wrote:
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.
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Kyle Peterson wrote:
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get
a quicker response by posting to the newsgroup.


--
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"
Dec 17 '05 #11
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.

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:O9**************@TK2MSFTNGP09.phx.gbl...
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

Scott wrote:
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.
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Kyle Peterson wrote:
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.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get
a quicker response by posting to the newsgroup.


--
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"

Dec 17 '05 #12
scott wrote:
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.
Dec 19 '05 #13

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

Similar topics

11
by: propizzy | last post by:
Appreciate any help!!! PROBLEM: I have this form that allows the user to dynamically create additional fields (see javascript code bellow). I am trying to retrieve the values entered into these...
2
by: Tommy Lang | last post by:
Hi everybody! I am trying to learn the basics of C++ myself and have a hard time understanding some stuff like pointers and references etc. I have created a small program that adds two numbers...
1
by: Tommy Lang | last post by:
I am trying to learn to use dynamic variables. I have pasted the code below. Is this the proper way of using dynamic variables? Thanks, Tommy ...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
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...
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...
1
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.