473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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:

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
2 20609
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
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

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

Similar topics

9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
12
by: Maurice | last post by:
Hi, is it possible to enter a string in code in VB.NET 2005 which will be executed as a codeline during runtime? i.e. I want to declare a new form depending on the string: "Dim frm as new...
2
by: radykl | last post by:
Can anyone explain me why you need to add open and close parenthesis to a JSON text in order to use eval() to parse it? For example: var json = "{a: 'abc', b: 'def'}"; var obj1 = eval("(" +...
3
by: ray_usenet | last post by:
Why is this: eval('') is OK, but eval('{"active":"true"}') tells me I'm missing ";" ? Then why is it that if I put parentheses around the second statement:
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
7
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new...
7
by: Ciaran | last post by:
Can someone please give me a hand with this? $arr=array(2,4,5,6); $test='arr'; echo (eval("$".$test."")); I'm trying to get it to echo 2
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.