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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic variable reference

Status
Not open for further replies.

dpimental

Programmer
Jul 23, 2002
535
US
Anyone know of a way to do the following.

I have 4 variables
Code:
var1 = "Red"
var2 = "White"
var3 = "Blue"
var4 = "Green"

I want to do a for loop from 1 to 4 and print each variable in turn, such as this

Code:
for i = 1 to 4
debug.print var & i
Next

I know this doesn't work, but how do I reference a variable in this way? Can I reference it somehow?

David Pimental
(US, Oh)
 
Why not using an array ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I guess what I am talking about is dynamically calling procedures and functions. Can you have an array of function names tht you can call by referencing the array index?

David Pimental
(US, Oh)
 
Any chance you could CLEARLY explain what you want to do ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Oh, if you insist.

I have several modules who's name differs only in last character, which is a number.

The idea is that different modules get called conditionally or at different times. I would like to be able to call them dynamically, not explicitly.

The modules look up info from a table and print values into a msword document bookmark.

But I want to be able to dynamically call the module based on conditioning without explicitly calling ...
Code:
Call getPersonalInfo1
execute additional code
...
Call getPersonalInfo2
execute addional code
...
Call getPersonalInfo3
... etc
I know if may sound funky, but I am working with a database application that I did not design, and I have 25 different modules in groups of 5, with each member of the group ending in the same number. So, you can see how advantageous it would be to automate the calling of these modules. I have tried doing the following.

Code:
for i = 1 to 5
Call Religion & i
Call Politics & i
Call Entertainment & i
Call Sports & i
Call Leisure & i
next

rather than ...

Code:
Call Religion1
Call Politics1
Call Entertainment1
Call Sports1
Call Leisure1 

execute code ...

Call Religion2
Call Politics2
Call Entertainment2
Call Sports2
Call Leisure2

execute code ...

Call Religion3
Call Politics3
Call Entertainment3
Call Sports3
Call Leisure3
etc

David Pimental
(US, Oh)
 
If your "modules" are functions you may try to play with the Eval function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top