Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

ms access - VBA how to run a string as a line of code

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...