Run the string as a statement in VB6

Posted on 2005-04-20
Medium Priority
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

ID: 13822000
LVL 52

Accepted Solution

Carl Tawn earned 500 total points
ID: 13822291
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.

Expert Comment

ID: 13823485
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

ID: 13824125
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 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