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

Looping thru variables -Need function to get value of string

Status
Not open for further replies.

Kenhardwick

Programmer
Nov 2, 1999
16
US
I have say five variables named, field1,field2,field3,field4 and field5.
And, I want to loop thru those five variables and want to do so thru code, like...

field1 = 5
field2 = 1
field3= 3
field4= 4
field5 3
total= 0

For cnt = 1 to 5
nextField = field & cnt
Total = Total + newfunction(nextField)
next

NewFunction(nextfield)
NewFunction = ???? value of the variable as indicated by the string passed...
(such as if nextfield = field1 then what is the value of
field1..ie 5)
end function

Would like to know if there is a way to write this "newfunction" such that it would return the value of
the variable named in the string passed...


Thanks for help on how to do this or any suggested other ways...Ken Hardwick,Norman,Ok
 
Consider the use of an array for this type of application:

dim myArray(5), total, i
myArray(1) = 1
myArray(2) = 5
myArray(3) = 4
myArray(4) = 2
myArray(5) = 3
total = 0

for i = 1 to 5
total = total + myArray(i)
next

penny.gif
penny.gif
 
where are these variables coming from? i've incremented the variable name as you describe in form fields, if your variables originate from a form, i can show you how to do it, but the way you have it now:

nextField = field & cnt

'nextField' will become the concatenation of the variable 'field' and the variable 'cnt'. field currently is empty, so the value of 'nextField' will become the value of 'cnt'

give us more information and we can probably give you some more ideas to work with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top