Eval scope in Access VBA

Please help.

=Does not work:

Function f()
    f = 1
End Function

Private Sub Button1_Click()
    k = Eval("f()")
End Sub

=Does not work:

Private Sub Button1_Click()
    с=1
    k = Eval("с")
End Sub

=Do work:

Function f()
    f = 1
End Function

Private Sub Button1_Click()
    k = f()
End Sub

=In help: The following example assumes that you have a series of 50 functions defined as A1, A2, and so on. This example uses the Eval function to call each function in the series.

Sub CallSeries()
    Dim intI As Integer
    For intI = 1 To 50
        Eval("A" & intI & "()")
    Next intI
End Sub 

How to make variant 1 work?

Thanks in advance.

++++++++++++++++++++++++++++

=UPDATE: The number of error I get with "Does not work" sections is 2425. "Can not find the name of the function." (or "Can not find the name of the expression." in the second case). Sorry for my English.

=UPDATE: When I try explicitly name the function in Eval:

 k = Eval("[Forms]![Form1].f()")

I get error 31005 - "Access can not calculate expression because there is a reference to "f"". I start to be suspicious of Access prohibiting use of user-defined functions and variables in Eval. Though help states opposite.

=UPDATE: Do work:

    Function f()
        f = 1
    End Function

    Private Sub Button1_Click()
        k = Eval("val(""551" & "9" & "6"")")
    End Sub

Very strange rules of double-quoting, as for me. But this does not solve my problem.

Upvotes: 2

Views: 3858

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112512

The Eval function cannot access functions that are in Form modules. Place the function in an ordinary Module.

Upvotes: 5

There is one interesting fact. Eval() does not accept user variables as arguments, but accepts form controls data.

Upvotes: 1

Related Questions