Part IV: Professional Database Development
950
The “API Examples” section, later in this chapter, provides typical wrapper functions for the API
calls documented in this chapter. All that’s needed to utilize an API call is to include the API func-
tion’s Declare statement, as well as the VBA wrapper function accompanying the API declaration.
Then call the VBA wrapper function as you would any function such as Format() or Now().
VBA wrappers protect your application from the sometimes ugly and tedious code needed to prop-
erly process API calls.
API Examples
Here comes the fun part. At the beginning of this chapter, I reviewed several different uses for the
Windows API. From here to the end of the chapter, you’ll see some examples of different types of
APIs, how to declare them, what you would use them for, error messages you may encounter, and
so on.
On the CD-ROM
All the examples in this chapter are included on the companion CD-ROM in the Chapter27.accdb example
database. The code examples in Chapter27.accdb are contained in a module named basAPIFunctions.
This module contains all the raw declarations and working functions you see described in this chapter.
The System Information dialog box (shown in Figure 27.1), which you open from the Access 2010
Options menu, contains useful information, including information about the operating system and
the computer’s hardware. The code in basAPIFunctions uses Windows API calls to populate
the API Demo form (frmAPIDemo) included in the Chapter27.accdb example database (see
Figure 27.2).
Caution
Whenever you’re working with API functions, frequently save your work. Because you’re working deep within
the Windows system, application errors can occur — especially when you’re first learning how to make these
calls.
Many Windows API functions use a parameter named hwnd. Hwnd is a long integer value that Windows
uses to keep track of graphical objects on the computer’s screen. Every object on the screen (windows,
buttons, text boxes, and so on) has an hwnd value, and many API calls require this value before they
can operate on the object.
Microsoft has made hwnd very easy to get for most Access objects. Many Access objects include hwnd
among their properties. You won’t see hwnd in the property window, but it’s there just the same. Use
Me.hwnd to pass the hwnd of an Access form, or MyControl.hwnd (for example, txtLastName.
hwnd) to pass the hwnd of a control to an API call requiring this important value.
What is this “hwnd” thing?