Sunday, June 17, 2012

Contoh Menjalankan Procedure Di dalam Script Control

Option Explicit

Private Sub Command1_Click()
ScriptControl1.Modules.Add Text1.Text
Form_Activate
End Sub

Private Sub Command2_Click()
ScriptControl1.Modules(List1).AddCode Text1.Text
List1_Click
End Sub

Private Sub Command3_Click()
Dim RetVal As Variant, m As Variant
Set m = ScriptControl1.Modules(List1.Text)
With m.Procedures(List2.Text)
Select Case .NumArgs
Case 0
RetVal = m.Run(List2.Text)
Case 1
RetVal = m.Run(List2.Text, 5)
Case 2
RetVal = m.Run(List2.Text, 4, 23)
Case Else
MsgBox "Procedure has too many arguments"
End Select
If .HasReturnValue Then
MsgBox List2.Text & " returned: " & RetVal
End If
End With
End Sub

Private Sub Form_Activate()
Dim m As Variant
List1.Clear
With ScriptControl1
.Language = "VBScript"
.AllowUI = True
For Each m In .Modules
List1.AddItem m.Name
Next m
End With
End Sub

Private Sub Form_Load()
Command1.Caption = "Add Module"
Command2.Caption = "Add Code"
Command3.Caption = "Run Procedure"
End Sub

Private Sub List1_Click()
Dim m As String, p As Variant
m = List1
List2.Clear
If m = "" Then Exit Sub
For Each p In ScriptControl1.Modules(m).Procedures
List2.AddItem p.Name
Next p
End Sub

Private Sub List2_Click()
Dim m As String, p As String, r As Boolean, a As Long
m = List1
p = List2
With ScriptControl1.Modules(m).Procedures(p)
r = .HasReturnValue
a = .NumArgs
End With
MsgBox m & "." & p & " has " & IIf(r, "a", "no") & _
" return value and " & a & " arguments"
End Sub

'Tambahkan module dan prosedur di bawah ini pada script control
Function Calc(X)
Calc = X * 2
End Function

Function Calc(X, Y)
Calc = X * Y
End Function

Sub Test()
MsgBox "The Test Sub in Module Mod2"
End Sub