By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

Dynamically execute a Sub, possibly with help of "ScriptControl"

P: n/a
[The language is VB6, but as it's about implementing dynamic call of
another method from the same module with the ScriptControl object, it
shouldnt matter too much..]

Hi,

I'd like to do something like that (see explanations after the code
snippet) :

Sub mainSub (foo as String)
Dim msScript As New ScriptControl
msScript.Language = "VBScript"
Dim someVar as String 'that'll be a parameter for the secondary sub
someVar = "blah"
Dim statement As String 'that's gonna be the dynamic statement to
execute
statement = foo & "_otherSub " & blah
'"reality check" call, equivalent to what I want, but static:
two_otherSub "blah"
'The thing...:
msScript.execute(statement) 'Using: foo = "two"
end Sub

Sub one_otherSub (bar as string)
'do something
end Sub

Sub two_otherSub (bar as string)
'do something
end Sub

I'd like to use that because:
mainSub takes a table name as parameter, fills a recordset with it and
exports it to a flat file. Now, if I can have different subs for the
formatting of the FIELDS of the recordset (when I write to file) and
call these subs dynamically depending on the table name parameter, then
I don't need anymore hard code in the mainSub... (like a big ugly
SELECT CASE with 100 branches)

Is that possible? Am I on the right track? I made some tries but
without success, an hour of googling without much success either.
Besides, I don't know whether I should use the .Eval, or .Run or
..Execute method..

If it helps to picture the case, here are the signatures:

1) moduleName.export(tableName As String)
The calling statement (in its "passive" form):
For Each fld In rs.Fields
TABLE_NAME_formatFields outFile, fld
Next

2) TABLE_NAME_formatFields(outFile As moduleName.outFile, fld)
Here's how it's handeld:
With outFile
Select Case fld.Name
Case "FIELD_1": .AddToLine 0, fld.value, 5
Case "FIELD_2": .AddToLine 2, fld.value, 25
'etc. for each field..
End Select
End With

NB: outFile's type is a class I made, where .AddToLine is working like
this:
AddToLine(spaceBefore as Integer, content as String, fieldLength as
Integer)

Thanks, bye!

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
One day, sj******@gmail.com wrote:
[The language is VB6, but as it's about implementing dynamic call of
another method from the same module with the ScriptControl object, it
shouldnt matter too much..]

<snippedy-doo-dah>

On the contrary, it's a one-liner in VB .NET!

You may have better luck asking your question in one of the VB-classic
groups, microsoft.public.vb.*

Hope this helps!

--
Tom Spink
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.