Problem with eval function

N

NH

x-posted to:
microsoft.public.access.formscoding, microsoft.public.access.modulesdaovba


I am trying to run some 'user definable' code on a form using the Eval
function:

This works fine:

myCode = """This "" & ""is "" & ""a "" & ""test"""
Forms!MyForm.MyField = Eval(myCode)

But what I want is:

myCode = """Forms!MyForm.MyField"" & "" = "" & ""Test"""
Eval(myCode)

Am I doing something wrong, or is there another function capable of doing
what I want?

Can anyone suggest another way of doing it?

Thanks,

Nick
 
M

Marshall Barton

NH said:
I am trying to run some 'user definable' code on a form using the Eval
function:

This works fine:

myCode = """This "" & ""is "" & ""a "" & ""test"""
Forms!MyForm.MyField = Eval(myCode)

But what I want is:

myCode = """Forms!MyForm.MyField"" & "" = "" & ""Test"""
Eval(myCode)

Am I doing something wrong, or is there another function capable of doing
what I want?


Eval evaluates an expression and returns its value, Eval can
not execute a statement. Only procedures in a module object
can execute VBA statements.
 
T

Tim Ferguson

NH said:
myCode = """Forms!MyForm.MyField"" & "" = "" & ""Test"""
Eval(myCode)

I don't know if Eval can even see controls on forms, but it if did, I guess
this would return False most of the time. If you want to set the value of a
form control, then just do it.

strFormName = "MyForm"
strControlName = "MyField"
strValueToSet = "Test"

Forms(strFormName).Controls(strControlName).Value = strValueToSet


There probably are such things, but I have not yet come across a legitimate
reason for using Eval... <ducks>

Hope that helps


Tim F
 
N

NH

Thanks Tim,

I agree. I don't think eval is going to help me here.

Your way is much closer to what I want, but I need to get the code from a
string:

What I want is to effectively 'run' a string, I.e.

myStr= "If (Forms!MyForm.Text1 <> 1) then Forms!MyForm.Text2 = 2"
<run> myStr

I could break the string down into components, e.g.

strFormName = "MyForm"
strControl1 = "Text1"
strControl2 = "Text2"
strValueToSeek = "1"
strValueToSet1 = "2"

...and then use your suggestion..

If Forms(strFormName).Controls(strControl1).Value = strValueToSeek then
Forms(strFormName).Controls(strControlName).Value = strValueToSet

But the user's rule could be <>1 , =1, <1 , >1...........

That's where I am having trouble..



You see, I need my users to be able to add very simple rules into a table,
and then their rules run when the form is closed...
 
M

Marshall Barton

If you break it down to its component parts and the
statement is always the same style, then you can use Eval to
deal with the user's rule:

strFormName = "MyForm"
strControl1 = "Text1"
strControl2 = "Text2"
strUserRule= "<>1"
strValueToSet = "2"

If Eval("Forms!" & strFormName & "." & strControl1 _
& strUserRule) Then
Forms(strFormName).Controls(strControlName) _
= strValueToSet
End If
 
N

NH

Ahh, there IS a reason to use Eval() after all...

I think that could work.

Thank you both for your help.

Nick

Marshall Barton said:
If you break it down to its component parts and the
statement is always the same style, then you can use Eval to
deal with the user's rule:

strFormName = "MyForm"
strControl1 = "Text1"
strControl2 = "Text2"
strUserRule= "<>1"
strValueToSet = "2"

If Eval("Forms!" & strFormName & "." & strControl1 _
& strUserRule) Then
Forms(strFormName).Controls(strControlName) _
= strValueToSet
End If
--
Marsh
MVP [MS Access]


What I want is to effectively 'run' a string, I.e.

myStr= "If (Forms!MyForm.Text1 <> 1) then Forms!MyForm.Text2 = 2"
<run> myStr

I could break the string down into components, e.g.

strFormName = "MyForm"
strControl1 = "Text1"
strControl2 = "Text2"
strValueToSeek = "1"
strValueToSet1 = "2"

..and then use your suggestion..

If Forms(strFormName).Controls(strControl1).Value = strValueToSeek then
Forms(strFormName).Controls(strControlName).Value = strValueToSet

But the user's rule could be <>1 , =1, <1 , >1...........

That's where I am having trouble..

You see, I need my users to be able to add very simple rules into a table,
and then their rules run when the form is closed...


of
a
 

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