Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a Procedure Where the Name = A Variables Value

Status
Not open for further replies.

Galford

Programmer
Jan 5, 2001
13
0
0
US
Is it possible to call a procedure using the contents of a variable as the name of the procedure? Kinda of like this

Dim StrProcedure as String

Call StrProcedure

The reason I'm trying to do this is because I've got a monster Select...Case statement that I'd like to get wrid of. It's for a security module that checks the group a user belongs to(There are 18) and what stage a document is at(i.e. Pending Director's Approval)(There are 10). Looks something like this:

Select Case Group
Case 1
Select Case Stage
Case Pending Director
Call Procedure1
End Select
Case 2
Select Case Stage
Case Pending Senior Director
Call Procedure2
End Select
etc..

If anyone could help on this, or suggest an alternate way of trimming the Select...Case down, I'd greatly appreciate it.
 
If the functions/methods were part of an object interface, you could use the CallByName method. I don't think, however, that CallByName works on non-object interface methods. - Jeff Marler
(please note, that the page is under construction)
 
If your procedure is (or can be turned into) a Function returning a string or a number, you can use the Eval() function:
Call Eval(StrProcedure & "()")
Eval() would return the result from your function, but you probably have no use for it.

Note: Eval() uses late binding to determine the target procedure at run time. As with any late binding, you'll take a performance hit. Rick Sprague
 
Rick,
What Library did you reference to get the Eval function inside of Visual Basic? I checked MSDN and the Eval seems to be an object method in MS Office and Visual FoxPro, but I do not see that function inside of Visual Basic 6.0 (with the standard VB6 EXE project references).

- Jeff Marler
(please note, that the page is under construction)
 
Whoops! I knew this would happen some day!

Most of the forums I work in are Access-related, and sometimes as I move into the VB forums I forget where I am. Eval() is provided by the Access library, not VBA.

Sorry about that! (Too bad, though--it's an elegant solution to this type of problem.) Rick Sprague
 
Yeah, it would've been pretty cool of it had worked with VBA. AS I mentioned before, the only way I know of (using pure VB code) to call a function/method by a string representation of its name is to use to CallByName function. BTW, this also works if you want to access properties by a string representation of the variable name. - Jeff Marler
(please note, that the page is under construction)
 
Thanks to both of you. This definately points me in the right direction. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top