?
Solved

RE:How to Execute String?

Posted on 2003-03-11
17
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 53

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 53

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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