Adding Code to the Scripting Engine

There are two types of code you can add to the scripting engine: subroutines and module-level variable declarations. Figure 13.2 shows how you would add a global variable, and Figure 13.3 shows how to add a function. Note that you don’t need to declare types for the variables or functions, because the only type available is Variant.

Figure 13.2: Adding a global variable

Figure 13.3: Adding a function

When you click the AddCode button, the event shown in Listing 13.1 is triggered. The AddCode method of the ScriptControl is used to add the contents of the large text box. This method will perform any necessary syntax checking, and it will make the code available for use with the Eval, ExecuteStatement, and Run methods. I use the On Error Resume Next statement to prevent compile-time errors in the script code from creating runtime errors in the main program.

Listing 13.1: Command7_Click Event in MSScript Demo

Private Sub Command7_Click()
On Error Resume Next
ScriptControl1.AddCode Text1.Text
End Sub

TIP: All at once or a little at a time—the choice is yours: You can add all of your code in a single shot or load each declaration or routine separately. Which approach you choose should be based on how you plan to let your users edit the code. If they are supposed to edit each subroutine independently of the others, then you should probably load each routine separately. If you’re planning to let users modify a series of declarations and routines, adding them all at one time is better.

© 1998 SYBEX Inc. All rights reserved.