Solved

Calling a sub / function which name is built at runtime

Posted on 2001-08-18
26
327 Views
Last Modified: 2012-05-04
Hi ,

is it possible to call a sub / function in VB if I build its name at runtime?
Let's say I have 3 subs  like

Private sub s1() 'or s2() or s3()
'do some stuff
End sub

and I have a string var MyStr which could have a value of "s1","s2" or "s3"

How can I call a sub which name is the same as MyStr value

I know I can do something like
If MyStr = "s1" Then
Call s1
Else
......
End If

but I don't want this.
I want to call a sub / fuction which name is defined at runtime as a string(or something else).
BTW is there in VB something similar to javascript's 'eval' functionality? The answer seems to be No, but anyway :)
0
Comment
Question by:hveld
  • 8
  • 7
  • 5
  • +3
26 Comments
 
LVL 5

Expert Comment

by:jklmn
ID: 6401473
Hi,

in VB you can do
Print ScriptControl1.Eval(2 + 3)
Is this what you want in you 'BTW'?
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6401482
Ooops, I meant

Print ScriptControl1.Eval("2 + 3")
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401497
jklmn is right. But you need MSscript control. You can download it from MS site.

Then you can create runtime sub/functions and also use that great Eval method like jklmn said.

suat
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 5

Expert Comment

by:jklmn
ID: 6401516
To smozqur:

Microsoft Script Control 1.0 should be available in VB6 from Components dialog box.
In fact, I didn't answer the first part of the question, can you answer that? I am listening....
0
 
LVL 5

Accepted Solution

by:
jklmn earned 100 total points
ID: 6401538
Hi hveld,

This can do what you want, though you might not like:-)
You need to put your Subs into the Script:

Private Sub Command1_Click()
Dim MyStr As String
MyStr = "s1"
ScriptControl1.Run "s1"
End Sub

Private Sub Form_Load()
Dim s As String
s = "sub s1()" & vbCrLf & _
"msgbox ""this is sub s1()""" & vbCrLf & _
"end sub" & vbCrLf
ScriptControl1.AddCode s
s = "sub s2()" & vbCrLf & _
"msgbox ""this is sub s2()""" & vbCrLf & _
"end sub" & vbCrLf
ScriptControl1.AddCode s
End Sub
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6401542
Sorry, that should be
ScriptControl1.Run MyStr
0
 
LVL 12

Expert Comment

by:roverm
ID: 6401548
Hi!

I think it is impossible to do that, maybe with API's or subclassing it's possible...but I think not.

Maybe it is possible to use something like
AddressOf MyStr and then call that ?

Looking into it...

D'Mzzl!
RoverM
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401550
Private Sub Command1_Click()
Dim Mystr As String
Mystr = "s1" 'The name of the sub in string variable
Call MainCode(Mystr)
End Sub

Private Sub MainCode(strSubName As String)
ScriptControl.AddCode ("Private Function RunSub()" & vbCrLf & "'Call (" & strSubName & ")" & vbCrLf & "Msgbox ""subname=" & strSubName & """" & vbCrLf & "End Function")
ScriptControl.Eval ("RunSub(" & Mystr & ")")
End Sub

sure
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401553
I am sorry. Wrong window :)

suat
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401554
No no, right window :) twice.

suat
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401561
Oh, similar but jklmn is fastest.

Congratulations

suat
0
 
LVL 12

Expert Comment

by:roverm
ID: 6401571
Wow ! Didn't think of that!
Way to go jklmn and suat (or is it sure now :>)!!

D'Mzzl!
RoverM
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6401592
"sure" was for ", can you answer that? I am listening...." of jklm.

:)
suat
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6401691
To smozqur,

Sorry, but I really want something else from you or others. The posted aproach might not be accepted by the questioner since it need to put all Subs, Functions (with 'vbCrLf's) into the Script Control. Another aproach that the questioner doen't like is simpler:
If MyStr = "s1" Then
Call s1
Else....

To roverm,

>AddressOf MyStr and then call that ?
That is for some APIs, can we do that in the code we written?




0
 
LVL 12

Expert Comment

by:roverm
ID: 6401762
jklmn:
I don't know, I used this when I build a project with several API timers. The SetTimer API calls an module.function/sub by using AddressOf.
I know that AddressOf can get the address of a Sub, Function or a Property. But I don't know if we can use this with a string or how to call it.

hveld:

Why don't you want to use something like:
Select Case MyProc
Case "S1"
  Call S1
Case "S2"
  Call S2
Case Else
  Call S3
End Select

D'Mzzl!
RoverM
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6401879
If S1, S2 and S3 are public methods of an object (a form for example), you can use CallByName to call them.
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 6402057
yes say for example you have a form 'Form1' with in it a sub 'public sub S1()'
then you can call it like so:
CallByName Form1, "S1", VbMethod
if the sub is 'public sub S2(parameter)'
then you can call it like so:
a = CallByName(Form1, "S2", VbMethod, parameter)
myvariable=parameter
note that 'parameter' is a variant
0
 
LVL 12

Expert Comment

by:roverm
ID: 6403224
The best answers can be soooooooo easy....:>

Way to go emoreau!

D'Mzzl!
RoverM
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6403243
I agree...

suat
0
 
LVL 6

Expert Comment

by:pierrecampe
ID: 6403293
Hi roverm
D'Mzzl!
is EE dan vergeven van de hollanders ?
geintje :-)
en natuurlijk van een belg

0
 
LVL 12

Expert Comment

by:roverm
ID: 6403491
Ik denk het ! ;-)
Pierre: You're the second to understand my signature!

D'Mzzl!
RoverM
0
 

Author Comment

by:hveld
ID: 6403510
Thanx jklmn!

I din't know you can build entire functions as strings at runtime and execute them, this will help me a lot.
Of course pierrecampe's answer seems to be the best and simpliest one for my question (as I asked it) , but this will do a much better job for what i'm trying to do, that's why I accepted it.
0
 

Author Comment

by:hveld
ID: 6403918
There is one problem I didn't notice when tested
If I build a Sub from string and run it,
I can't set any variables, looks like they remain internal for the newly generated Sub, although I declared them as public in a module. Here is an example

in module1
-------------
Public a As String

in form1
--------------
Public Sub Command1_Click()

a = "Test NOT OK"

s = vbCrLf & "Public Sub test()" & vbCrLf & _
"a= ""TestOK""" & vbCrLf & _
"MsgBox a" & vbCrLf & _
"End sub" & vbCrLf

ScriptControl1.AddCode s
ScriptControl1.Run "test"

MsgBox a
End Sub

a retains a "Test NOT OK" value although it's set to "TestOK" in the generated Sub. Do you have an idea how to set variables from a sub created this way?

0
 
LVL 5

Expert Comment

by:jklmn
ID: 6408127
Hi hveld,

You should get "TestOK" firstly and then get "Test NOT OK" later.
That is correct...because the code displays the value of a local variable 'a' inside Sub test() firstly....then displays the value of a public variable 'a' later...
What is wrong and what you need?
0
 
LVL 5

Expert Comment

by:jklmn
ID: 6408271
Hi hveld,

Sorry....now I know what you meant....but got no idea how to do what you want....seems you need to use function to return a value.....
Any one got suggestions?
smozgur, any idea:-)
0
 

Author Comment

by:hveld
ID: 6411574
Yes, it works if I use a function, like this:

Public Sub Command1_Click()
Dim a As String, b As String
a = "Test NOT OK"

s = vbCrLf & "Public function test()" & vbCrLf & _
"a= ""TestOK""" & vbCrLf & _
"MsgBox a" & vbCrLf & _
"test = a" & vbCrLf & _
"End function" & vbCrLf

ScriptControl1.AddCode s
b = ScriptControl1.Eval("test")
MsgBox b
End Sub

But no way with sub's
BTW is it possible for someone to itercept commands I send to a script control, and the values returned?
For example can someone see the function test(),eventually the parameters it's called with, and what it returns?
Microsoft's script debugger can not, but is there some other possible way?
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question