Link to home
Start Free TrialLog in
Avatar of dosdemon
dosdemon

asked on

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.
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Base_the_Bass
Base_the_Bass

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