×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Executing code

Executing code

Executing code

(OP)
I want to be able to execute a line of code that has been typed into a text box by a user (say form1.show) when a button is pressed. I've tried placing text1.text in the click event of the button, however that does not work. Any ideas or work arounds would be greatfully appreciated.

RE: Executing code

first of all you would have to make sure that users were aware of the names of the events they are trying to trigger. YOu would have to use a series of if..then statements to check the text in text1. If the if...then statement finds a match you could fire of the event.

private sub Command1_Click()

if text1.text = "form1.show" then
form1.show
elseif text1.text = "end" then
else
msgbox "Not an executable string", vbinformation
end
end if

end sub

RE: Executing code

(OP)
Ok
Say there is a valid event in the textbox ie form1.show , is there any way of executing that without looping through the "if.. then" routine??

RE: Executing code


I think that you should consider using "VBScript".

A colleague demonstrated a VB5 app to me. A form, a textbox and a button. Type into the text box click the button and it executed (interpreted) the code. No need to predict the code entered with the "if ... then" stuff above.

My colleague can't help at the present as he has his hands full. But why not visit http://msdn.microsoft.com/scripting as a starting point. You'll need to download the Microsoft Script Control.

RE: Executing code

My colleague says...

By using the vbscript control, you can expose objects in your application to the global namespace.

So you would expose your form as say theForm and in the textbox type

sub()
theForm.Show
end sub

On your lostfocus event on the textbox or a button click , you should take the text and get the script control to execute it, and hey presto, Bob's your aunt's live in lover,
it should all work. It also allows you to save the vbscript to file, to be loaded back in at run time and executed if you wish, thus giving you a fully customisable environment

Hope this helps


Mark Ruse

RE: Executing code

I just tried it and it works.
Here's an example.

In a new project drop a ScriptControl(see below), 2 Labels, 2 big TextBoxs & 2 CommandButtons onto a new form. Then paste in the following code
------------------------------------------------------------
Option Explicit

Private Sub Command1_Click()
ScriptControl1.Language = "VBScript"
ScriptControl1.AddCode (Text1.Text)
ScriptControl1.Run ("Test")
End Sub

Private Sub Command2_Click()
ScriptControl1.Language = "VBScript"
ScriptControl1.ExecuteStatement (Text2.Text)
End Sub

Private Sub Form_Load()
'Expose the form as frmMainWindow
ScriptControl1.AddObject "frmMainWindow", Me
Text1 = "sub Test()frmMainWindow.label1=""hello"" end sub"
Text2 = "frmMainWindow.label2=""world"""
End Sub
------------------------------------------------------------
The script control can be found, amongst other places, on the VB6 CD in \common\tools\vb\script. The readme.txt is quite straightforward.

Sean.

RE: Executing code

(OP)
I cant thank U enough for such a comprehensive answer !!!
Cheers.
Honda

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close