Err.LastDLLError Property

Syntax

Err.LastDLLError

Description

A read-only property containing a long data type representing a system error produced within a DLL called from within a VB program.

Rules at a Glance

  • Only direct calls to a Windows system DLL from VB code assign a value to LastDLLError.

  • The value of the LastDLLError property depends upon the particular DLL being called. Your code must be able to handle the various codes that can be returned by the DLL you are calling.

  • Don't forget that a failed DLL call doesn't itself raise an error within your VB program. As a result, the Err object's Number, Description, and Source properties aren't filled.

Programming Tips and Gotchas

  • The LastDLLError property is used only by system DLLs, such as kernel32.dll, and therefore errors that occur within DLLs you may have created in VB won't be assigned.

  • Obtaining accurate documentation about the return values of system DLLs can be a challenging experience. Most useful information can be found by studying the API documentation for Visual C++. However, you can use the Windows API FormatMessage to return the actual Windows error message string from within Kernel32.DLL, which incidentally is also in the correct language. The following is a brief example you can use in your applications to display the actual Windows error description:

    Option Explicit Declare Function FormatMessage Lib "kernel32" _ Alias "FormatMessageA" _ (ByVal dwFlags As Long, lpSource As Any, _ ByVal dwMessageId As Long, ...

Get VB & VBA in a Nutshell: The Language 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.