Run the string as a statement in VB6

Hi,
I am making a VB6 form application. I want to be able to "run" a string, i.e.:

strSayThis = "Hello"
strDoThis = "MsgBox(strSayThis)"
Run(strDoThis) ' when tihs statement is run, it will display a messagebox saying "Hello"

I want to know how to do this "Run" function.
If it helps, its about the same function as "SetTimeout" in JavaScript.

Thanks for your help.
dosdemonAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Add a reference to the "Microsoft Script Control" library from the Project > References menu. Then add the following code to the form:

    Private Sub Form_Load()

        Dim sCommand As String
        sCommand = "MsgBox(""Hello World"")"

        Call RunCmd(sCommand)

    End Sub

    Private Sub RunCmd(ByVal pCmd As String)

        Dim oScr As New ScriptControl
   
        oScr.Language = "VBScript"
        oScr.ExecuteStatement pCmd

    End Sub


Hope this helps.
0
 
DhaestCommented:
0
 
Base_the_BassCommented:
Actually Dhaest alsmost hit the spot

what I would do is this
make a Public Sub that contains what you need to do:

Public Sub DOSDemon (SayThis as String)
msgbox(SayThis)
End Sub

now you can call this sub by a string anywhere in your code:
Private Sub Form_Activate()
DoThis="DosDemon"
SayThis="Whatever"
CallByName Me,Dothis,vbMethod,SayThis
End Sub

This works.
Explanation:CallByName is a command that lets you run(call is the proper expression) any property or method of an objext(or form if you prefer).
Msgbox IS NOT a method of the form,that's why you can't run it from CallByName,so you need to make a Method that contains the msgox command(A sub you create in a form is a method of the form)
However you can call immediately other methods of the form from aString,eg CallByName Me,"Cls",vbMethod

To see what's a method and what's not,press F2.That should bring the object browser.Now type form in the search box.You can see the methods and properties of the form.
0
 
JR2003Commented:
This will do the job for you:

Private Sub Command1_Click()
    Dim ScriptControl As Object
    Dim strDoThis As String
    Dim strSayThis As String
    strSayThis = "Hello"
    strDoThis = "MsgBox(""" & strSayThis & """, 64)"
    Set ScriptControl = CreateObject("MSScriptControl.ScriptControl")
    ScriptControl.Language = "vbScript"
    ScriptControl.Eval strDoThis
End Sub
0
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.

All Courses

From novice to tech pro — start learning today.