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

Error using Eval() on a concatenate variable name.

Status
Not open for further replies.

TKaminski

Programmer
Sep 5, 2001
27
US
I have a long series of global variable names where the last character is a number.

Example:
MyVariableName1,
MyVariableName2,
MyVariableName3, etc.

I am trying to cycle through this long series using a loop and then read the value of each variable.

Code:
Dim intLoopCounter As Integer
Dim strTempVariableName As String
Dim strTempValue As String

intLoopCounter = 1

Do Until intLoopCounter > 500
	
	strTempVariableName = _
        “MyVariableName” & intLoopCounter


	‘***NOTE: Code error is on this next line***
	strTempValue = eval(strTempVariableName)


	‘	code here uses strTempValue


intLoopCounter = intLoopCounter +1
Loop

The error message is:
“Microsoft Access can’t find the name ‘MyVariableName1’ you enter in the expression.”

The error type number is:
“2482”

May goal is to have strTempVariableName read not a text but the value of the variable that this name represents.

The global variables are properly declared and work fine in other areas of the code, so I know that part is working.

Any help on this line of code, or a better way to do this would be helpful.

Thanks for reading.
 
Hmm, have you considered using an Array for your variables, so for instance, say you have a totoal of 10 variables, you could just do something like:
Code:
Dim MyVariableName(10) As [i]whatever[/i]
Code:
Dim intLoopCounter As Integer
Dim strTempVariableName As String
Dim strTempValue As String

intLoopCounter = 1

Do Until intLoopCounter > 500
    
    strTempVariableName = _
        [b]MyVariableName(intLoopCounter)[/b]


    ‘***NOTE: Code error is on this next line***
    strTempValue = eval(strTempVariableName)


    ‘    code here uses strTempValue


intLoopCounter = intLoopCounter +1
Loop



Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Actually, if you had 10 variables, then you could dim your array more like this to be accurate:

MyVariableName(9)
or
MyVariableName(1 to 10)

I believe...

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
TKaminski,
I don't believe you should be using the Eval function. That specifically, is causing your problem.
I'm not 100% clear on its use, but it returns a value based on a function, basicaly,(I think)...
This alone, will give you the answer you want...
strTempValue = strTempVariableName.

Like KJV, my first reaction is that, strTempvalue gets erased, after each loop. "code here uses strTempValue" solves that I suppose?

Hope this helps, Good Luck!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top