• Problem with eval

    Author
    Topic
    #469287

    Hi
    I’ve got some code-testing code that needs to use the eval command to evaluate strings as code.
    I’ve narrowed it down to actions that use array types. Here’s a one liner that fails:


    b = Eval(“ubound(Array(1, 2, 3))”)

    It throws run-time error 2425 – “The expression you entered has a function name that Microsoft Access can’t find”
    Of course, this works fine in Interactive mode:
    ? ubound(Array(1, 2, 3))
    2

    Strangley, if I use Evaluate I get a slightly different error:

    b = Evaluate(“ubound(Array(1, 2, 3))”)
    ? b
    Error 2029

    What’s the difference between eval and Evaluate?

    It’s in Access 2002; don’t know whether it’s a problem in other versions or other applications.
    Any ideas welcomed.

    Peter

    Viewing 8 reply threads
    Author
    Replies
    • #1226466

      If you highlight the function name Eval (or Evaluate) and press F1, do you get a specific page or the dialog indicating that it exists in multiple libraries, or an error?

      • #1227393

        If you highlight the function name Eval (or Evaluate) and press F1, do you get a specific page or the dialog indicating that it exists in multiple libraries, or an error?

        F1 on Eval takes me to Microsoft Visual Basic Help – Eval Method – You can use the Eval function to evaluate an expression that results in a text string or a numeric value. Variant.
        F1 on Evaluate takes me to Microsoft Visual Basic Help – Evaluate Method – Converts a Microsoft Excel name to an object or a value.

    • #1226525

      Eval is a method of the Access Application, Evaluate is a method of the Excel (and possibly others) application. Neither can be used to evaluate VBA like that.

    • #1227399

      OK, looks like I’ve got two problems.

      I don’t think Access works the way I want to, but for anyone else that’s ever wanted to know, here’s my findings.

      1. The eval method has it’s own scoping rules.
      eval can’t ‘see’ variables outside it’s scope. This isn’t a big problem, you can just build up the string. Consider the str function if you don’t have a string type.


      num = 2
      i = Eval(“5 + num * 2”) ‘FAILS

      i = Eval(“5 + ” & str(num) & ” * 2″)
      Debug.Print i ‘Prints 9

      2. eval doesn’t work with arrays.
      It just looks like there’s no support for arrays from within eval.


      Debug.Print Array(1, 2, 3)(2) ‘Prints 3
      Debug.Print Eval(“Array(1, 2, 3)(2)”) ‘Error

      Peter

    • #1227444

      As I mentioned, neither Eval nor Evaluate can be used to evaluate VBA.

    • #1227578

      Thanks Rory.
      What do you mean eval can’t be used to evaluate VBA?
      I’m using it all the time to do just that – it’s just I’m having trouble using eval with arrays.
      For example, this works fine:

      Code:
      num = 2
      i = Eval("5 + " & str(num) & " * 2")    'Prints 9
      

      Pete

    • #1227579

      Array(arg1,arg2,arg3,…) is a VBA function and eval cannot handle VBA functions or methods. It can do the regular math operations however.

      • #1227639

        Array(arg1,arg2,arg3,…) is a VBA function and eval cannot handle VBA functions or methods. It can do the regular math operations however.

        Not true. Access VBA can handle some (most?) functions, but not all. So this works fine:

        Code:
        Debug.Print eval("chr(65)")     'Prints A
        
    • #1227580

      What you are evaluating there is a straight expression (which would work in a query for example), not VBA.

      • #1227640

        What you are evaluating there is a straight expression (which would work in a query for example), not VBA.

        Sorry, I still don’t understand.
        Here’s some plain VBA using Eval, which works fine in Access 2002 and 2007:

        Code:
        Sub test()
            Dim num As Integer
            num = 2
            i = Eval("5 + " & Str(num) & " * 2")
            MsgBox i    'Displays 9
        End Sub
        
    • #1227653

      Your Eval statement there is not evaluating the str function, it is only evaluating “5 + 2 * 2” which is an expression (such as you can use in a query), not VBA.

      • #1227670

        Your Eval statement there is not evaluating the str function, it is only evaluating “5 + 2 * 2” which is an expression (such as you can use in a query), not VBA.

        True. But this example has eval evaluating a VBA function:

        Code:
        Debug.Print eval("chr(65)")     'Prints A
        
    • #1227688

      You are quite right! I was foolishly assuming that Eval and Evaluate work the same way, but they are actually VERY different.

    Viewing 8 reply threads
    Reply To: Problem with eval

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: