4

Is there a way to convert a string into an executable line of code?

something like:

Dim Line1 as String
Line1 = "MsgBox (""Hello"")"
Execute Line1

resulting in the pop up box saying Hello.

What I'm doing is pulling lines out of a text file. I can pull lines that are the names of form controls and use them to perform actions on those form controls but I'd like to go one step further and execute lines. I've seen claims that this would work:

Application.Run Line1

or make the variable an array and store it in element 1 e.g. and use

Application.Run Line1(1)

but it doesn't work for me.

Ok, while writing this I've also been experimenting. I found that

Eval (Line1)

will work when Line1 is a message box, but not when it is something like:

line1 = "DoCmd.OpenForm ""form1"""

Any tips would be appreciated.

Thanks

Erik A
  • 31,639
  • 12
  • 42
  • 67
charles_m80
  • 63
  • 1
  • 1
  • 9

3 Answers3

3

You can use,

 Eval("DoCmd.OpenForm(""form1"")") 

You have to make sure any functions you include use parentheses. Further reference, http://msdn.microsoft.com/en-us/library/office/aa172212(v=office.11).aspx

Jimmy Smith
  • 2,452
  • 1
  • 16
  • 19
  • Thanks for the tip on parentheses, but it still doesn't work. Putting in the code exactly as you wrote results in the run-time error 2482, "Database can't find the name 'DoCmd' you entered in the expression" – charles_m80 Jul 25 '14 at 15:04
  • I tested it on an existing database with a form1, using Access 2003. Can you put it into context for me and post the code that you added it to? Also, which version of Access are you using? – Jimmy Smith Jul 25 '14 at 16:12
  • To test the syntax I literally just tied the code to a new button: Private Sub Command118_Click() Eval ("DoCmd.OpenForm(""frmMain"")") End Sub There is already a button on the same form that successfully uses: DoCmd.OpenForm "frmMain" So I'm minimizing the potential variables. I'm in Access 2007. If I change it to "Application.DoCmd..." in the Eval, I get a message about DoCmd not being safe in sandbox mode, but adding the same "Application." to the functional button works fine. I guess it's possible DoCmd won't work with Eval in sandbox mode. – charles_m80 Jul 25 '14 at 18:17
0

It's not exactly what I was asking, I ended up going a slightly different direction, but here's what I ended up doing and it would probably be easily altered to more closely match my question. I actually took lines of text from an external text file and inserted them into a line of code. What I was doing in this example was just hiding columns, the external text file was a list of column names. (figuring out how to output that was fun too)

Open "C:\UserList.txt" For Input As #TextFile
While Not EOF(TextFile)
Line Input #TextFile, TextLine
Screen.ActiveDatasheet.Controls(TextLine).ColumnHidden = True
Wend
charles_m80
  • 63
  • 1
  • 1
  • 9
0

Visual Basic is a compiler language, and as such does not support the ability to execute human-readable code while it is running. All code written in VBA must first be compiled BEFORE the program runs the first time. However, SQL is an interpreter language, and can be handed code which it will execute line by line. You can also fetch contents for variables from other sources while the program runs, you just can't build a string while the program is running and then execute it in VBA directly.

Nerobyrne
  • 25
  • 1
  • VB .Net is a pre-compiled language. VBA is something else. It is interpreted and it has the function ExecuteLine. – y4cine Jul 28 '22 at 15:57