Microsoft Access 2010 Bible

(Rick Simeone) #1

Part IV: Professional Database Development


946


How to Use the Windows API


If you’ve made it this far, congratulations! The concept of APIs can be a little intimidating. Once
you understand the concepts, however, using them is as easy as calling any other function or sub-
routine from Access VBA.

The Declare statement
In order to use an API in your Access application, you must first tell Access the name of the API
function and where to find it. You do this within the declarations section of a module using the
Declare statement. The Declare keyword notifies VBA that what follows is not part of VBA but
exists outside of the current application. A prototype Declare statement is shown here:

Declare [Function|Sub] FunctionName Lib “LibraryName” _
Alias “AliasName” (ArgumentList) As DataType

The Declare statement has several parts, all of which are discussed in the following sections.

Note
The Declare statement is sometimes referred to as an API function prototype because it serves as the proto-
type for all calls to the API function.


Function or Sub
APIs can be in the form of a function or a subroutine, just like Access VBA procedures. A function
returns a value back to the calling code, while a subroutine does not. When an API function
returns a value, good programming practice requires the calling procedure to check the return
value to verify that the function completed as expected. The vast majority of Windows API calls are
functions, so you rarely have to deal with the distinction between functions and subs.

Function name
The function name you use in the Declare statement can be one of two things:

l The actual name of the API function you’ll be using as declared in the library: For
instance, if you were going to use GetPrivateProfileString, listed in the “Making
sense of the documentation” section, earlier in this chapter, you would use
GetPrivateProfileStringA as the function name.
l The name of the function as you would like to use it within your code.

Tip
GetPrivateProfileStringA is a long function name, especially if you’re going to be using the function
frequently within your code. You might want to shorten its name to GetString instead. You can do this by
using the Alias parameter discussed in the “Alias ‘AliasName’” section, later in this chapter.

Free download pdf