Access™ 2007 VBA Programmer's Reference
by Teresa Hennig, Rob Cooper, Geoffrey Griffith, Armen Stein
14.5. Err.LastDLLError
Like the VBA procedures you write, API procedures can also generate errors. These can be the result of bad or missing data, invalid data type assignments, or a variety of other conditions or failures. This section describes how to trap and retrieve API-generated errors so you can take remedial or other action to shield the user from their adverse effects.
LastDLLError is a property of the VBA Err object. It returns the error code produced by a call to a DLL, and always contains zero on systems that don't have DLLs (like the Macintosh).
DLL functions usually return a code that indicates whether the call succeeded or failed. Your VBA code should check the value returned after a DLL function is called and, on detecting a failure code, should immediately check the LastDLLError property and take whatever action you deem necessary. The DLL's documentation will indicate which codes to check for.
Because no exception is raised when the LastDLLError property is set, you cannot use the On Error Goto construct, so use On Error Resume Next.
The Err object's Description property will be empty because the error is DLL-specific. However, for many error messages you can use the FormatMessage API to get the error message returned by a DLL. Add this code to the module containing the SetFormIcon procedure:
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Declare Function FormatMessage Lib "kernel32" Alias _ "FormatMessageA" (ByVal dwFlags As Long, lpSource As Long, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access