Executing a string

M

Magius96

In short, what I need is a way to execute a string as VBA code that will
allow the code in the string to modify variables within the program. I have
added the Microsoft Scrip Control 1.0 into the references, and have tried the
following code with no luck:

Function test()
Dim CodeString As String
Dim SCtrl As New ScriptControl
SCtrl.Language = "VBScript"
TestVar = 1
SCtrl.ExecuteStatement ("TestVar = TestVar + 9")
MsgBox TestVar, vbOKOnly
End Function

I need to find a way to do this, without actually knowing what variables are
going to be modified in advance. A program that I'm working on will have
different code for every entry in a table, and I want to be able to store the
code itself in the table and execute it when necessary.
 
K

Klatuu

Use the Eval function:
Function test()
Dim CodeString As String
TestVar = 1
CodeString = "TestVar = TestVar + 9"
Eval(CodeString)
MsgBox TestVar, vbOKOnly
End Function
 
M

Magius96

I tried that, and recieved the error message:

Run-time error '2482':

Test DB can't find the name 'TestVar' you entered in the expression

I used the code:

Function test()
Dim CodeString As String
Dim TestVar As Integer
TestVar = 1
CodeString = "TestVar = TestVar + 9"
Eval (CodeString)
MsgBox TestVar, vbOKOnly
End Function
 
K

Klatuu

My mistake, I answered too fast.
It will not work that way with memory varialbes. I have tried several
different ways, but none seem to work.

I will let you know if I come up with something.
 
M

Michel Walsh

Use a function which returns your variable, or
FORMS!anOpenFormNameHere!OneOfItsControlNameHere:


? eval( "5+toto()")
11


with

======================
Option Compare Database
Option Explicit

Public Function Toto() As Long
Toto = 6
End Function
======================



or


? eval( "5+FORMS!form1!text0")
228


with Form1 open and its control text0 having a confirmed value of 223 (by
confirmed, I mean you are not still typing the number, ie, you have left the
control text0 so the control after update event has been executed, if there
is one, as example).




Vanderghast, Access MVP
 
K

Klatuu

Interesting, but I don't see how it applies. The OP is asking about using an
Eval with a memory variable, but the name of the memory variable is not know.
 
M

Michel Walsh

Instead of using a variable, he can use a function, which is, for all
practical purposes, a name (of a variable) followed by a pair of
parenthesis.


Vanderghast, Access MVP
 
M

Magius96

I don't see how it could work. The application being built uses hundreds of
variables, and any one of those variable can be effected. What's needing to
happen, is the formula that determines what variable to modify and how to
modify it will be housed in a table. Yeah, I could easily write this mess
into a function, but I want to keep it in the table so that part is easily
scriptable by the end user.

From my research, and what you guys have said, I'm determining that what I'm
trying to accomplish just isn't possible with Access and VBA, unless I
completely write my own scripting language inside the app.
 
K

Klatuu

Maybe if you describe the objective, we could help with some ideas.

What is it, exactly, you are trying to make happen?
 
M

Magius96

To make it simple, the database will actually be an RPG game. One of the
tables will contain information for every object (Item, armor, weapon, ect)
in the game. Some of these items will have special scripts to be run at a
certain point in the game as determined by the "ScriptType" field.

For instance, one item may be a weapon that freezes the opponent when struck
with a critical strike, so the script for it may be "EnemyAgility = 0" while
it's script type is "Critical_Strike"

Another item may grant the player 100 xp when used, so it's script would be
"PlayerXP = PlayerXp + 100;call CheckExp()", while it's ScriptType would be
"OneUse"

In effect, the script would be housed in the table as a string. I know how
to get the script from the string, and how to run the script. The problem
I'm facing is getting the script to use and modify memory variables.

Like you stated before, Eval() won't do that, and all of my research proves
that it can't be done. Which means to me that I'll have to go with an
alternate plan that would involve more fields in the table, and a lot more
hard-coding than I was desiring, or will have to code in my own scripting
engine.

I was trying to use a scriptable approach so it would be easier to add new
items and effects later down the road. The overall objective here is to make
the game easy to update without requiring me to actually edit the VBA code
just to add a new item/effect/spell.
 
K

Klatuu

Okay, here is my idea. Use one or more Class modules.
You can use the Intiailize event of the class module to read the attributes
for each object into the class and estabish the initial values for the
objects.

Then your code can use property gets and property lets and even property
sets to manipulate the objects and their values. This would be faster than
having to read data from tables for each instruction.
 
M

Michel Walsh

You may also define your scripts as VBA functions.


As example, if an item increase the Armor Protection by 10 for 5 minutes,
you can store, in some table:


ItemID Script args ' fields
1010 "ArmorClass" "10, 5" ' data


and then, if playerID 45 use the item:

eval( "Set" & rstItems!Script & "(" 45 & ", " & rstItems!args &
")" )


wich will be, just before entering in the eval function:

eval( "SetArmorClass(45, 10, 5)" )


and your function:

Public Sub SetArmorClass( PlayerID AS long, IncreasedAC AS double,
DurationTime AS double )
...
End Sub


will be able to modify the required global variables / objects as it fits.
Sure, in the table of 'events to come' you will have to add something like


rstEvents!ScriptToRun = "Remove" & rst!Script & "(" & 45 & rst!args &
")"
rstEvents!WhenToRun = DateAdd( "m", 5, now)





You may also find the VBA function CallByName useful for some situations.




Vanderghast, Access MVP
 
M

Michel Walsh

.... make it DateAdd("n", 5, now). "m" is for month; "n", for minutes.

Vanderghast, Access MVP
 
M

Magius96

Now THAT is a solution I can live with. Thank you all so much for helping me
out with this. I don't know why I didn't think of it that way myself, but I
guess sometimes it just takes an outside perspective.
 

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