Microsoft KB Archive/210511

= ACC2000: How to Call Functions by Using a String Variable =

Article ID: 210511

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



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:

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

Option Explicit

  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  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.

