VBA how to run a line as a line of code

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

sort of:

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

as a result, a pop-up window will appear with the message "Hello".

What I am doing is pulling lines from a text file. I can pull out the lines that are the names of the form controls and use them to perform actions on these form controls, but I would like to take another step and execute the lines. I saw statements that this would work:

Application.Run Line1

or make the variable an array and store it in element 1, for example. and use

Application.Run Line1(1)

but it does not work for me.

Well, while writing this, I was also experimenting. I found that

Eval (Line1)

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

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

Any advice would be appreciated.

thank

+3
3
+3

Visual Basic - , , , , . , VBA, , . SQL , . , , VBA.

+1

This is not quite what I asked, I ended up going in a slightly different direction, but here is what I did in the end, and it will probably be easily changed to more closely fit my question. I actually took lines of text from an external text file and put them in a line of code. What I did in this example was just hiding the columns, the external text file was a list of column names. (figuring out how to output this was interesting too)

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

Source: https://habr.com/ru/post/1674039/


All Articles