Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 27: Using the Windows Application Programming Interface


947


Lib “LibraryName”
The library name is simply the name of the DLL that contains the API function or subroutine that
you’re declaring. This parameter tells Access where to find the function. If the DLL is not a stan-
dard Windows DLL, or the DLL has been moved to another location, you’ll have to specify the
complete path of the DLL. The LibraryName parameter must be enclosed in quotations but is
not case sensitive.

Most Windows DLLs are located in the System32 folder within the main Windows folder (usu-
ally C:\Windows). If the DLL path is not given in the Declare statement, the VBA interpreter
first looks for the DLL in the System32 folder, and then in the Windows folder. In the rare event
that you’re using a DLL located anywhere else on your computer, you should specify the path to
the DLL as part of the LibraryName clause in the API function’s declare statement.

Alias “AliasName”
The Alias clause lets you call an API function by some other name, if needed. In such a case, the
FunctionName parameter is the new name you assign to the function, and the Alias clause
identifies the original function name. In the following example, the Declare prototype is prefixed
with api to indicate that it is an API function call, and not the usual VBA function name:

Declare Function apiGetPrivateProfileString _
Lib “Kernel32” Alias “GetPrivateProfileStringA”

In this example, apiGetPrivateProfileString is the function name you’ll use in your VBA
code, and it’s an alias for GetPrivateProfileStringA.

There are several reasons why using an alias is a good idea. In the preceding example, the alias
provides a way to apply a naming convention (the api prefix) to the declared API function.
Occasionally, you’ll encounter API functions that begin with an underscore, such as _lopen or _
lread. Access VBA procedures cannot begin with an underscore, so these functions must be
aliased. Another reason to alias your function names is to avoid the possibility of declaring a func-
tion using a name that already exists in your Access application or in its libraries. If you try to
declare a function with a name that already exists, you’ll receive an error: Ambiguous Name
Detected: FunctionName.

Tip
Like library names, the aliased function name (GetPrivateProfileStringA, in this example) must always
be enclosed in quotations. The alias reference (GetPrivateProfileStringA) is always case sensitive and
must be spelled exactly like the function name it references in the DLL.


ArgumentList
The ArgumentList is composed of the elements that the function expects to receive from you in
order to do its job. When you declare a function, you must declare the same number of arguments
in the function declaration that the function’s documentation specifies. If you don’t, you’ll receive
runtime error 49: Bad DLL calling convention. The same error occurs if you pass incompat-
ible arguments to an API function.
Free download pdf