Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

Modules: Use variables to call functions

Author(s)
Dev Ashish

Q)    How can I call a Function by using a variable instead of Function Name?

(A)    Use the Eval Function. If you pass to the Eval function a string that contains the name of a function, the Eval function returns the return value of the function. For example, Eval("Chr$(65)") returns "A".

So for example, in the following code,  if you call fEval with "A" as parameter, you should get the result "Test That", else "Test This".

'*******Code Start*******
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fEval(status As String)
Dim strFunctionName As String
Dim x
    If status = "A" Then
        strFunctionName = "TestThat()"
    Else
        strFunctionName = "TestThis()"
    End If

    fEval = Eval(strFunctionName)
End Function

Function TestThis()
    Debug.Print "Test This"
End Function

Function testThat()
    Debug.Print "Test That"
End Function
'*******Code End**********

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer