using dynamic variables with EVAL in MS ACCESS 2003

S

SOLICON

I want to asign TRUE to a variable named Okay in the following way:

Dim Okay as Boolean
Dim VarName as String

VarName = "Okay"

Eval("VarName") = True
Eval("VarName = True")

It gives both the run-time error 2482 cannot find 'VarName'

Is there a way to use dynamic variables in MS ACCESS 2003?

Many Thanks, André
 
B

Bill Mosca, MS Access MVP

You are really going about it completely wrong. Eval is a function that
evaluates an expression that results in a string or a numeric value.

Dim bolOkay as Boolean
BolOkay = True

If you are looking to assign a string based on a boolean's value if would
look as follows:
Dim bolOkay As Boolean
Dim strName As String

bolOkay = True
strName = CStr(bolOkay)
'Prints "True"
Debug.Print strName

bolOkay = False
strName = CStr(bolOkay)

'prints "False"
Debug.Print strName


--
Bill Mosca, MS Access MVP


I want to asign TRUE to a variable named Okay in the following way:

Dim Okay as Boolean
Dim VarName as String

VarName = "Okay"

Eval("VarName") = True
Eval("VarName = True")

It gives both the run-time error 2482 cannot find 'VarName'

Is there a way to use dynamic variables in MS ACCESS 2003?

Many Thanks, André
 
A

Albert D. Kallal

No, then again, I not as a rule needed to do this.

I did a good multi-year stint working in FoxPro...the "last" dos version
make (2, or version 2.6..can't remember right now).

Anyway, in FoxPro, we had a feature in which "code", or expressions could
represent values. It was certainly a very cool and handy feature.

On the other hand, I been writing FAR MORE code intensive and FAR MORE
complex application in ms-access then I ever did in that old version of
FoxPro.

The amazing part is that I NOT missed this ability ms-access to "substitute"
string code.

What this means is that you might wan tot suggest what exactly you are
trying to accomplish instead of trying to build re-time code that modifies
it self.

The #1 REASON WHY I not missed "macro" substitution feature of FoxPro in
ms-access is because virtually EVERYTHING in ms-access is a collection. And,
all collections CAN BE resolved with strings (expressions) in ms-access at
runtime.

further, since tables are collections in ms-access, in place of storing this
value in a variable, why not store it in a table? (or a custom collection).

I can't recommend any design in ms-access now that needs you to resolve
variable names at runtime, as we simply don't need to do this anyway....


If you favourite a table defined, and use a reocrdset, then the fields
(values) are simply a collection.

strWhatFieldName = "Company"

Msgbox "the comapnaye name is " & rstCustomers(strWhatFieldName)

the above msg box would display the company name field. so, this is why I
mentioned that I not missed this "substitution" feature that FoxPro had, and
if you design your software with the above knowledge in mind, you not need
to resolve to variables at runtime at all......

I think with a "minor" change in your design, you can avoid all of your
problems....
 
S

SOLICON

Hi John,

What I need is the following:

I have a Table with 2 fields.

One field represents the name of an existing boolean public variable.
The second field is the value (true or false).

With that table I want to set a number of boolean variables.
I use these variables to give some users more or less authority to some
fields in some Forms.

In the login I check the segurity group, and depending on the name I
trace a part of the table.

For the time being I read the variable names from the table and check
them in a SELECT CASE block.

Thanks, André

John Nurick ha escrito:
 
J

John Nurick

Hi André,

I'd probably use a little VBA function to get the values from the table
and call it whenever necessary rather than have a bunch of global
booleans to manage. I.e. instead of
Public gBlSomeSetting
...
gBlSomeSetting = GetSetting("SomeSetting")
...
If gBlSomeSetting Then
...

I'd just do
If GetSetting("SomeSetting") Then
...


Air code alert:

Public Function GetSetting(SettingName As String) As Boolean
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT [Value] FROM tblSettings " _
& "WHERE SettingName = '" & SettingName "';"
Set rs = dbEngine(0)(0).OpenRecordset(strSQL, dbOpenSnapshot)
If rs.EOF Then 'no record for this setting
GetSetting = False
Else
GetSetting = cBool(rs.Fields(1).Value)
End If
rs.Close
End

But there are other possibilities.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top