13.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 these 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.

Since no exception is raised when the LastDLLError property is set, you cannot use the On Error Goto construct; therefore, use On Error Resume Next.

Modify the SetFormIcon procedure to generate a DLL error by passing an empty string as the icon path. This shows the LastDLLError property in action.

Public Sub SetFormIcon(hWnd As Long, strIcon As String) Dim hIcon As Long Dim lngReturn As Long On Error Resume Next 'Pass an empty string as the icon path hIcon = LoadImage(0&, "", IMAGE_ICON, IMG_DEFAULT_WIDTH, _ IMG_DEFAULT_HEIGHT, LR_LOADFROMFILE) 'Now check for an ...

Get Access 2003 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.