Get Your Own Free News Ticker  




  Home

  Articles

  Links

  Contact Me

  HLR eBooks

 

Executing Visual Basic Code From a File

Download Source Code & Executable

Perhaps you wish to build plug-in type capability into your code or make it easy to alter functions after your application has been developed. Being able to run Visual Basic from an external database or file would be a simple way of doing this and could very likely speed up development for small projects. I’m waffling now however so here is how to do it below.

First off you need to open the database or file and extract the visual basic code from it. Obviously to store the code in a file simply save the desired function into a new file. With a database save the code into a field of type memo. This tutorial will cover extracting the code from a file for simplicity.

Private Function GetCodeFromFile(Filename as String) as String
Dim strLine as String

Open Filename For Input as #1
While EOF(1)=False
Line Input #1, strLine
GetCodeFromFile = GetCodeFromFile & strLine & vbCrLf
Wend
Close #1
End Function

Build a form similar to the one below. The extra control you need added to the form is the Microsoft Scripting Control found in the components dialog. If you don’t already have it get it here.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/scriptinga.asp





In the form_load function we need to briefly set up the control:

Private Sub Form_Load()
' Add the object which the script has access to
ScriptControl1.AddObject "Text1", Text1, True
ScriptControl1.AddObject "Text2", Text2, True
ScriptControl1.AddObject "Text3", Text3, True
...

This small section of code allows the visual basic inside the file access to these three controls, Text1, Text2, and Text3. If you wanted access to other objects just add them in the same way.

To read the code in from a file and add it to the control enter this code in the Load Plugin command button.

' actually load it in
pluginCode = GetCodeFromFile(FileNames.Text & ".txt")

' add the code to the script control
ScriptControl1.AddCode pluginCode

This simply uses our earlier created file reading procedure and then adds the code to the script control. Our control is now ready to perform its desired function, although we still need some files to load. I created two files which add and multiply the two text boxes respectively and give the answer in the bottom text box.

These are the two files. Please remember though that the files are actually written in VBScript, which means that the syntax is closer to Microsoft Access than Visual Basic 6. However this shouldn’t be too much of a problem as they still have access to Visual Basic controls.

Add.txt
Public Sub pluginCode()
' cint(...) converts a string to an integer
Text3.Text = cInt(Text1.Text) + cInt(Text2.Text)
End Sub

Multiply.txt
Public Sub pluginCode()
' cint(...) converts a string to an integer
Text3.Text = cInt(Text1.Text) * cInt(Text2.Text)
End Sub

So finally. One thing left to do. Make the ‘Do It’ command button execute this code. Inside the command buttons onclick code type this:

' Run subroutine pluginCode without any parameters
ScriptControl1.Run "pluginCode"

Run the program, load the plugin, click the button and we're done...

Download Source Code & Executable











Free website templates