RE:How to Execute String?

Hi every one, I just want to know in VB does have any execute string command?
for example :

Dim sString as string

sString = "Msgbox 'Hello world'"

can  I execute this string to get my msgbox?
I know in Java can do it, so how about VB?
Thanks you and waiting for prompts reply.
sjchinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SilentRageCommented:
No, the closest you can get is executing VBScripts using the script control.
0
SethiCommented:
Simply put the string you want to execute in a procedure and then call the procedure whenever required. Example:
Private sub callMe(strString as String)
msgbox strString
End Sub

Now whenevrer you require a new msgbox with new string simply pass the string to this procedure and call the procedure.
0
SilentRageCommented:
He's not asking a newbie question.  He wants to execute VB code that may be located in an external file or supplied by user input.  This wouldn't be limited to message boxes.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sjchinAuthor Commented:
My problem is not only at msgbox function. Maybe the string contains form, text box or other controls objects.

For example :

Assume we have formA and FormB

In formA, normally we can simply used FormB.show to show FormB. How about "FormB" name we store in String due to some condition of my program, How can i Execute the "FormB.show"?

Dim sString  as string

sString = "Form1"


I want execute sString & ".show" to show Form B.
Thanks you for your concern and wait for your reply. :>


0
SilentRageCommented:
and I told you that there is no way to do what you ask.  The closest thing is executing VBScripts.
0
Arthur_WoodCommented:
dim MyForm as Form
strForm = "Formb"

Set MyForm = Forms.Load(strForm)

MyForm.Show


This wil allow you to pass the NAME of a form to be loaded and shown, as a string.

But as SilentRae said, in general, there is NO WAY to execute arbitrary VB command lines from within another VB program.

AW
0
avya2kCommented:
you can insert script control in form and execute as follows
scriptcontrol1.eval(UR String here)
0
sjchinAuthor Commented:
avya2k , May i know how to add scriptcontrol to a form? Which OCX or DLL you are using? Thanks you.
0
Ryan ChongCommented:
Exactly the same as what i posted for other PAQ questions:

Try add the M$ Scripting Runtime from your Reference, then implement the example below:


Dim iScript As New ScriptControl
Dim a As Variant
Dim aa As New clsScript

Private Sub Command1_Click()
   On Error GoTo EH
   iScript.Language = "VBScript"
   a = iScript.Eval(Text1.Text)
   MsgBox a
   Exit Sub
EH:
   ShowErrMsg
End Sub

Private Sub Command2_Click()
   On Error GoTo EH
   iScript.Language = "VBScript"
   iScript.Reset
   iScript.AddObject "aa", aa
   iScript.ExecuteStatement Text2.Text
   MsgBox aa.a
   Exit Sub
EH:
   ShowErrMsg
End Sub

Private Sub Command3_Click()
   Randomize
   iScript.Language = "VBScript"
   iScript.Reset
   iScript.AddObject "Command3", Command3, True
   iScript.ExecuteStatement "Command3.Caption = " & Chr$(34) & "Hello World" & Rnd * 1 & Chr$(34)
End Sub

Private Sub Form_Load()
   Command1.Caption = "Eval"
   Command2.Caption = "ExecuteStatement"
   Text1.Text = "(5500 * 0.986)* 12/100"
   Text2.Text = "x = 5500" & vbCrLf
   Text2.Text = Text2.Text & "aa.a = (x * 0.986)* 12/100"
End Sub

cheers
0
sjchinAuthor Commented:
ryancys,
It seem your code will work, which can call VBScript.
I want to know which ScriptControl you are using?
I have add Reference -->Microsoft Scripting Runtime(SCRRUN.dll), it appear user -defined not defined. May I know is the dll i using is wrong?
Thanks and wait for your reply :>
0
Ryan ChongCommented:
No, it's the correct dll, that's scrrun.dll in your System folder.

Donno why it's not working for you. By the way, try download the M$ Scripting Runtime file from the M$ website, try re-install it see how goes first. (sorry, i forget the URL but it's scripten.exe)

regards
0
avya2kCommented:
you are doin the wrong thing pal
go in Projet\Componets and click on
Microsoft Script Control V1.0
then you can have it done
Enjoy!!!!!
0
avya2kCommented:
Add Script Control from ToolBox in your form
and instead of iScript use ScriptControl1
0
sjchinAuthor Commented:
avya2k,
 I am goto www.microsoft.com to download Microsoft Scripting Runtime as ryancys say, but it seem like still can't work (the code as ryancys type) after i install. I also try to find the component as avya2k said, but nothing in my components.
I just want to know is Win98 don't have such component? My development PC is Win98 2nd Edition, VB with SP5. Thanks you guys and wait for prompts reply :>
0
avya2kCommented:
Provided u have installed VB6 properly >>
Go in Project Menu Click on Components Browses for msscript.ocx and add it to ur form
if u don't find this visit avya.8m.com and find it in downloads
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avya2kCommented:
r u there
0
sjchinAuthor Commented:
I have try several method, it still can't solve my problem. Anyway, Thanks lots help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.