How to use variables in EVAL?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!
In access I need to write something like that:

Dim A as Integer, B as String
B="A"
A=10

and I would like that Eval(B) give me 10.
But it does not work.
Please: how can I do that?
 
Hi, Gilbert.

In some programming languages, pointers to a variable can be used and your
code would be very close to working. If B were a pointer to an integer and
the name A were substituted for the string "A", then it would work in another
programming language.

In VBA however, there are no pointer data types, so you need to have both
variables defined as the same data type before the value of one of them can
be assigned to the other variable. Additionally, the value assigned to one
of the variables must be assigned prior to this variable being assigned to
the other variable. And since the Eval( ) function needs to be passed a
string expression, then the value of the integer must be converted to a
string before being passed to the Eval( ) function. For an example that
works, try the following code:

Dim A As Integer
Dim B As Integer

A = 10
B = A

MsgBox Eval(CStr(B))


HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts, so that all
may benefit by filtering on "Answered questions" and quickly finding the
right answers to similar questions. Remember that the best answers are often
given to those who have a history of rewarding the contributors who have
taken the time to answer questions correctly.
 
Gilbert said:
In access I need to write something like that:

Dim A as Integer, B as String
B="A"
A=10

and I would like that Eval(B) give me 10.
But it does not work.
Please: how can I do that?


The problem is that Eval does not know about VBA variables.
This means the you have to convert the A in string B to its
literal value:

Eval(Replace(B, "A", A))
 
Thank you... but it does not solve my problem!
In fact, in a batch program, I would like to trace all errors in a log file.
So, I have something like this (sorry for my english):

On error goto Error_log
Dim A as integer, B as integer,.., X as integer
Const Separator = "|"
Dim Cause_of_the_error as string, Precision_on_the_error as string

Cause_of_the_error = "Cause_1"
Precision_on_the_error = "A" & Separator & "B"
...
A = 10
...
B = 5
...
...
Cause_of_the_error = "Cause_2"
Precision_on_the_error = "C"
...
C = 10
...
...
Cause_of_the_error = "Cause_3"
Precision_on_the_error = "D" & Separator & "E" & Separator & "F"
...
D = 100
...
E = 5
...
F = 60
...
...
...
....

Error_log:
Memorize_error(..., Eval(Precision_on_the_error), ...)
Resume next

When I am in "error_log", I don't know where I come from, so it is very
difficult to konw what to replace...
 
Eval does not look at the list of variable names.

There is nothing you can do to or with Eval to make
it look at the list of variable names.

Eval only looks at the list of Function/Subroutine
names.

There are two approaches to what you want to do:
either create functions with the names you want,
or create a function with a list of the names you
want.

Function fnC
fnC = C
End Function

Function fnD
fnD = D
End function

or

Function EvalVar(sVarName)
select Case sVarName
"C": EvalVar = C
"D": EvalVar = D
etc

(david)
 
Well, you don't mention where the actual values are coming from?

Likely, that data has to come from somewhere (like a table).

Do NOTE that you CAN use a string variable name to reference any data field
you want. So, BEFORE you start shoving the data into variables...keep the
data in the table, or reocrdset you use for processing.

dim strWhatField as string

dim rstRecData as dao.recordset

set rstRecData = currentdb.OpenRecordset("select * from tableCustomers")

strWhatField = "LastName"

msgbox "the value of the last name field is " & rstRecData(strWhatField)

So, you can use collections here. And, in fact, you can build your own
custom collections, and use a string var again. So, really, you likely do
NOT need to use variables, and they are a poor choice here, as you can't
create new variables at runtime, and thus the idea of "substitution" for
variables don't make sense anyway from a software point of view (if you
don't know the name of the variable to be used, then how can you be use that
it exists in the first place!!).

So, since we do NOT have runtime creating of variable names, then trying to
resolve what variable name we used does NOT make sense.

You can use collections here, and for data tables, there is plenty of
workarounds to your problem here...

I worked in FoxPro (2.0 days in dos), and did like the "macro" substitution
feature. However, I worked for YEARS in ms-access, and due to the fact that
all data CAN BE referenced by string vars, then I have NEVER had to resort
to eval(), or the need to have a variable point to another variable, since
variables CAN point to data.

So, DO NOT put your log data into varalbes, but simply refernce the data in
the tables...
 
Gilbert said:
Thank you... but it does not solve my problem!
In fact, in a batch program, I would like to trace all errors in a log file.
So, I have something like this (sorry for my english):

On error goto Error_log
Dim A as integer, B as integer,.., X as integer
Const Separator = "|"
Dim Cause_of_the_error as string, Precision_on_the_error as string

Cause_of_the_error = "Cause_1"
Precision_on_the_error = "A" & Separator & "B"
...
A = 10
...
B = 5
...
...
Cause_of_the_error = "Cause_2"
Precision_on_the_error = "C"
...
C = 10
...
...
Cause_of_the_error = "Cause_3"
Precision_on_the_error = "D" & Separator & "E" & Separator & "F"
...
D = 100
...
E = 5
...
F = 60
...
...
...
....

Error_log:
Memorize_error(..., Eval(Precision_on_the_error), ...)
Resume next

When I am in "error_log", I don't know where I come from, so it is very
difficult to konw what to replace...


You're not going to be able to do this with VBA variables.
Eval can only recognize public functions, fully qualified
form/report controls and literal values.

That's why I said to replace the variable names with their
value. If worse comes to worst, you could loop through all
the variable names replacing all of them, even they're not
used.

I don't understand what you expect the | character to
accomplish. AFAIK, that character has no use in an
expression.
 
Back
Top