Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

RE:How to Execute String?

Posted on 2003-03-11
17
Medium Priority
?
159 Views
Last Modified: 2010-04-07
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.
0
Comment
Question by:sjchin
  • 5
  • 5
  • 3
  • +3
17 Comments
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116238
No, the closest you can get is executing VBScripts using the script control.
0
 
LVL 18

Expert Comment

by:Sethi
ID: 8116260
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
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116296
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sjchin
ID: 8116493
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
 
LVL 1

Expert Comment

by:SilentRage
ID: 8116675
and I told you that there is no way to do what you ask.  The closest thing is executing VBScripts.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8116798
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8116837
you can insert script control in form and execute as follows
scriptcontrol1.eval(UR String here)
0
 

Author Comment

by:sjchin
ID: 8117108
avya2k , May i know how to add scriptcontrol to a form? Which OCX or DLL you are using? Thanks you.
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8117214
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
 

Author Comment

by:sjchin
ID: 8117463
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
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8117507
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8125271
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8125293
Add Script Control from ToolBox in your form
and instead of iScript use ScriptControl1
0
 

Author Comment

by:sjchin
ID: 8133147
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
 
LVL 5

Accepted Solution

by:
avya2k earned 200 total points
ID: 8133790
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8149963
r u there
0
 

Author Comment

by:sjchin
ID: 8164390
I have try several method, it still can't solve my problem. Anyway, Thanks lots help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question