What Is a Callback Function?

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

To call most DLL functions from VBA, you write a VBA function that calls the DLL function, and you're done. To call a function that requires a callbackfunction, however, you write a VBA function that calls the DLL function, and the DLL function then calls another VBA function, the callback function, that you've defined in your project. You never call the VBA callback function directly; the DLL function calls it for you. The diagram in Figure 10.2 provides a conceptual overview of how a callback function works.

Figure 10.2 How a Callback Function Works

The DLL functions that require callback functions are often functions that must execute some action repeatedly. For example, the SetTimer API function requires a callback function each time a specified interval elapses. You might use it to update a clock on a form every second, or to display a form every hour. This is a simple task in Microsoft Access, which provides a Timer control for use on Access forms. The other Office applications, however, don't have Timer controls. You can use the SetTimer function to create a timer.

Other DLL functions that require callback functions are the enumeration functions, which enumerate through a group of Windows objects: EnumWindows, EnumPrinters, EnumFontFamilies, and so on. For example, the EnumWindows function enumerates through all of the existing windows and calls the callback function for each window. You can use the EnumWindows callback function to perform an operation on each available window, such as getting the window's caption text or determining its location on the screen. Procedures that call the EnumWindows function can be found in the EnumWindows.xls sample file, available in the ODETools\V9\Samples\OPG\Samples\CH10 subfolder on the Office 2000 Developer CD-ROM.