Run the string as a statement in VB6

Posted on 2005-04-20
Last Modified: 2006-11-18
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.
Question by:dosdemon
    LVL 53

    Expert Comment

    LVL 52

    Accepted Solution

    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.
    LVL 1

    Expert Comment

    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)
    End Sub

    now you can call this sub by a string anywhere in your code:
    Private Sub Form_Activate()
    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.
    LVL 18

    Expert Comment

    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

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction While answering a recent question ( 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…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    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…
    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…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now