ACC2000: How to Call Functions by Using a String Variable (210511)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210511
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article describes how you can call Visual Basic for Applications functions or user-defined functions when the function name is stored in a string variable. This method provides a functionality similar to that of pointers to functions in other programming languages.

MORE INFORMATION

To allow significant programming flexibility, you can call a function in Visual Basic for Applications when the function name is stored in a string variable. The Eval function used in the following example evaluates three strings as if they were expressions, and then returns their values:

  1. Start Microsoft Access and create a new, blank database.
  2. Create a module, and then type the following line in the Declarations section if it is not already there:

    Option Explicit

  3. Type or paste the following procedures:
    '------------------------------------------------------------------
    'GLOBAL DECLARATIONS SECTION
    '------------------------------------------------------------------
    
    
    '------------------------------------------------------------------
    ' The CallMyArray() function creates an array of strings, then
    ' loops, using the Eval() function, to call each element of the
    ' array.
    '------------------------------------------------------------------
    
    Function CallMyArray()
       Dim MyArray(), i As Integer
       
        For i = 0 To 2
           ReDim Preserve MyArray(i)
           MyArray(i) = "MyFunction" & i & "(" & i & ")"
        Next i
    
        For i = 0 To 2
            Eval (MyArray(i))
        Next i
    End Function
    '------------------------------------------------------------------
    '    The first function called by CallMyArray().
    '------------------------------------------------------------------
    Function MyFunction0(nParam)
       MsgBox "This is the output of MyFunction" & nParam
    End Function
    '------------------------------------------------------------------
    '    The second function called by CallMyArray().
    '------------------------------------------------------------------
    Function MyFunction1(nParam)
       MsgBox "This is the output of MyFunction" & nParam
    End Function
    '------------------------------------------------------------------
    '    The third function called by CallMyArray().
    '------------------------------------------------------------------
    Function MyFunction2(nParam)
       MsgBox "This is the output of MyFunction" & nParam
    End Function
    					
  4. Type the following line in the Immediate window:

    ? CallMyArray()

    Notice that you receive dialog boxes as the output of each of the three functions: MyFunction0, MyFunction1, and MyFunction2.

REFERENCES

For more information about the Eval() function, click Microsoft Access Help on the Help menu, type eval function in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/30/2004
Keywords:kbhowto kbinfo kbprogramming KB210511