Using Eval() function to treat string as VBA statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rdemarco
    New Member
    • Feb 2008
    • 5

    #1

    Using Eval() function to treat string as VBA statement

    Hi guys

    I'm trying to use the eval() function to run code that I have previously compiled via string concatenation.

    Some of it works, for example, I can set the focus to a combo box called 'AssetIDLookup' , which is on the other form, with this code:

    Code:
    'First building the string using variables:
    buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.setfocus"
    'Now use eval() and pass the string to have it converted to a VBA statement:
    Eval (buildCommandString)
    So far so good - the focus is successfully set and no errors. However, when I try to use the same method to set the text of that combo box, I keep getting the RunTime error described at the end of my post:

    Code:
    'building the string 
    buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = " & AssetID
    'and useing eval() to treat it as a vba statement:
    Eval (buildCommandString)
    Here is the Run Time error (2482):
    The application can't find the name 'Asset008' (the actual asset code from my variable) you entered in the expression.

    Using msgbox or debug to check the string confirms that the statement is correct. And hardcoding that statement into the procedure also confirms that it works, ie. the combo box is set to eg. 'Asset008'.

    Any help/hints would be greatly appreciated :-)

    Thanks
    Rob
  • rdemarco
    New Member
    • Feb 2008
    • 5

    #2
    Have not found a way to do it with the eval() function, but here is a workaround:

    Code:
     Screen.ActiveForm!AssetIDLookup.Text = AssetID
    It works, but I would still like to hear if anyone has a better idea :-)

    Thanks
    Rob

    Comment

    • SonyBytes
      New Member
      • Aug 2008
      • 1

      #3
      The problem with this one seems to be passing a literal without quotes so your application can't find the literal when it attempts to look for it, instead of assigning it as a text value to your box:
      Code:
      buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = " & AssetID
      Try adding single quotes, since you are building a string, like:
      Code:
      buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = '" & AssetID & "'"

      Comment

      Working...