O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required