Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2005

Tip: Looking for answers? Try searching our database.

How to use variables in EVAL?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gilbert LEFRANÇOIS - 06 Mar 2005 07:59 GMT
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?
'69 Camaro - 06 Mar 2005 17:01 GMT
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.

> Hi!
> In access I need to write something like that:
[quoted text clipped - 6 lines]
> But it does not work.
> Please: how can I do that?
Marshall Barton - 06 Mar 2005 17:31 GMT
>In access I need to write something like that:
>
[quoted text clipped - 5 lines]
>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))

Signature

Marsh
MVP [MS Access]

Gilbert LEFRANÇOIS - 07 Mar 2005 00:17 GMT
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...

> >In access I need to write something like that:
> >
[quoted text clipped - 11 lines]
>
>     Eval(Replace(B, "A", A))
Albert D. Kallal - 07 Mar 2005 03:44 GMT
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...

Signature

Albert D. Kallal   (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com
http://www.members.shaw.ca/AlbertKallal

Marshall Barton - 07 Mar 2005 06:20 GMT
>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.
[quoted text clipped - 38 lines]
>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.

Signature

Marsh
MVP [MS Access]

david epsom dot com dot au - 07 Mar 2005 01:44 GMT
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)

"Gilbert LEFRAN?OIS" <GilbertLEFRANOIS@discussions.microsoft.com> wrote in
message news:59DC73A4-4129-48CC-93DF-0FD240FC08F0@microsoft.com...
> Hi!
> In access I need to write something like that:
[quoted text clipped - 6 lines]
> But it does not work.
> Please: how can I do that?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.