n variables in a loop

mary.h

Registered User.
Local time
Today, 22:35
Joined
Aug 21, 2003
Messages
48
Hi all,

does someone know if it were possible to use the Eval function to do following, and if so - how to do it. Cause I am failed.

Thing is I store strings in 7 different variables. They are named

strValue1
strValue2
strValue3
...
strValue7

And for a specific output I want to join this values in one string. I know this is all very easy, but I thought it could be accomplished with the Eval function in a for - next - loop.

Code:
For i=1 to 7
strBuffer = Eval("strValue" & i)    'strBuffer should so have the value of strValue1 - if i = 1 
Endstring = Endstring & ", " & strBuffer 
Next

But if I do it with this Eval function, it says it doesent find strValue1. If I call Eval(strValue1) and strValue1 = "Hello", than it says there is no value "Hello".

Can anyone help. It's just that I love to learn.
Kind regards,
Mary.H
 
Have you tried simply:

strBuffer = strValue & i
 
Eval won't help you I'm afraid.
It's designed to evaulate expressions (it's an Access function - a member of the Application object).
Therefore it evaluates Access expressions - and that includes functions, VBA built in and user defined as the expression service can evaluate them - so Eval can.
It is, however, not capable of running code (which is what it would have to do were it used as you'd like - it would need a mini-compiler all it's own as opposed to being handed back function results from VBA).

You could force what you want by creating a dummy function that just returns that parameter passed to it... but that wouldn't help your loop (you can't construct variable names in VBA).
However I'd say you should just be using an array in this case.
It's classic stuff..

Code:
Dim strValue(1 to 7)
 
strValue(1) = "Something"
strValue(2) = "Something Else"
...
 
For i=1 to 7
    Endstring = Endstring & ", " & strValue(i) 
Next
 
Disregard my brain-cramp induced reply. I misread the OP.
 
Thanks.

I was afraid that Eval wont work. But it's good to know the point.

Of course the array solution was in my mind too.

Great and thanks.
Its Mary.H
 

Users who are viewing this thread

Back
Top Bottom