Visual Basic Programming Code Examples Visual Basic > API and Miscellaneous Code Examples Return the error message associated with Err.LastDllError Return the error message associated with Err.LastDllError The following code will return an error message associated with a particular DLL error number: Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long 'Purpose : Return the error message associated with LastDLLError 'Inputs : [lLastDLLError] The error number of the last DLL error (from Err.LastDllError) 'Outputs : Returns the error message associated with the DLL error number Public Function ErrorDescriptionDLL(Optional ByVal lLastDLLError As Long) As String Dim sBuff As String * 256 Dim lCount As Long Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400 Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF If lLastDLLError = 0 Then 'Use Err object to get dll error number lLastDLLError = Err.LastDllError End If lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0) If lCount Then ErrorDescriptionDLL = Left$(sBuff, lCount - 2) 'Remove line feeds End If End Function