By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Using Eval() function to treat string as VBA statement

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. 'First building the string using variables:
  2. buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.setfocus"
  3. 'Now use eval() and pass the string to have it converted to a VBA statement:
  4. Eval (buildCommandString)
  5.  
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:

Expand|Select|Wrap|Line Numbers
  1. 'building the string 
  2. buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = " & AssetID
  3. 'and useing eval() to treat it as a vba statement:
  4. Eval (buildCommandString)
  5.  
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
Feb 5 '08 #1
Share this Question
Share on Google+
2 Replies


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

Expand|Select|Wrap|Line Numbers
  1.  Screen.ActiveForm!AssetIDLookup.Text = AssetID
  2.  
It works, but I would still like to hear if anyone has a better idea :-)

Thanks
Rob
Feb 5 '08 #2

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = " & AssetID
  2.  
Try adding single quotes, since you are building a string, like:
Expand|Select|Wrap|Line Numbers
  1. buildCommandString = "Forms!" & AppropriateFormName & "!AssetIDLookup.Text = '" & AssetID & "'"
  2.  
Aug 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.