Get Error Messages Directly from the System

Here is a routine used to get error messages directly from the system instead of hard-coding them :

Sample :


       Call APIError

       ' *** Or
       Debug.Print ReturnAPIError(53)
       ' *** Return : The network path was not found.

' #VBIDEUtils#**************************************************
' * Programmer Name  : Waty Thierry
' * Web Site	 : www.geocities.com/ResearchTriangle/6311/
' * E-Mail		 : waty.thierry@usa.net
' * Date		 : 12/10/1998
' * Time		 : 20:20
' * Module Name	 : APIError_Module
' * Module Filename  : APIError.bas
' **************************************************************
' * Comments	 :
' * Used to get error messages directly from the
' *   system instead of hard-coding them
' *
' *
' **************************************************************

Option Explicit

Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200

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

' *** Status Codes Public Const INVALID_HANDLE_VALUE = -1& Public Const ERROR_SUCCESS = 0&

Public Function ReturnAPIError(ErrorCode As Long) As String ' #VBIDEUtils#*********************************************** ' * Programmer Name : Waty Thierry ' * Web Site : www.geocities.com/ResearchTriangle/6311/ ' * E-Mail : waty.thierry@usa.net ' * Date : 12/10/1998 ' * Time : 20:21 ' * Module Name : APIError_Module ' * Module Filename : APIError.bas ' * Procedure Name : ReturnAPIError ' * Parameters : ' * ErrorCode As Long ' *********************************************************** ' * Comments : ' * Takes an API error number, and returns ' * a descriptive text string of the error ' * ' ***********************************************************

Dim sBuffer As String

' *** Allocate the string, then get the system to ' *** tell us the error message associated with ' *** this error number sBuffer = String(256, 0) FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or _ FORMAT_MESSAGE_IGNORE_INSERTS, 0&, ErrorCode, 0&, sBuffer, Len(sBuffer), 0&

' *** Strip the last null, then the last CrLf pair if it exists sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1) If Right$(sBuffer, 2) = Chr$(13) & Chr$(10) Then sBuffer = Mid$(sBuffer, 1, Len(sBuffer) - 2) End If

ReturnAPIError = sBuffer

End Function

Public Sub APIError() ' #VBIDEUtils#*********************************************** ' * Programmer Name : Waty Thierry ' * Web Site : www.geocities.com/ResearchTriangle/6311/ ' * E-Mail : waty.thierry@usa.net ' * Date : 12/10/1998 ' * Time : 20:35 ' * Module Name : APIError_Module ' * Module Filename : APIError.bas ' * Procedure Name : APIError ' * Parameters : ' *********************************************************** ' * Comments : ' * Takes an API error number, and returns ' * a descriptive text string of the error ' * ' ***********************************************************

Dim sError As String On Error GoTo ERROR_APIError sError = InputBox("Enter the error number", _ "Returns API error") If IsNumeric(sError) = False Then Exit Sub MsgBox ReturnAPIError(CLng(sError)), vbInformation + vbOKOnly, _ "Error n " & sError Exit Sub ERROR_APIError: MsgBox "Error n " & sError & vbCrLf & _ " Invalid error number" & vbCrLf & _ "You have to give another one", _ vbCritical + vbOKOnly, "Error n " & sError End Sub



Comments

  • How to use unlimmited buffer

    Posted by Legacy on 08/27/2002 12:00am

    Originally posted by: yoram

    change lpBuffer to Byref lpBuffer as long
    so
    Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByRef lpBuffer As Long, ByVal nSize As Long, Arguments As Long) As Long

    use th flag
    Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100

    now
    Dim buffer as long
    FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_ALLOCATE_BUFFER, _
    0, msgId, 0, buffer, 0, 0

    now you post the string by
    ' Note that this is not vb msgbox but an API function
    MessageBox(0, buffer, Caption, mbType)
    end then
    ' Enother API function
    LocalFree buffer

    Reply
  • More on API errors and Visual basic

    Posted by Legacy on 02/14/2000 12:00am

    Originally posted by: Duncan Jones

    When testing for API errors in VB, do not use the GetLastError() api call as Visual basic itself may perform any number of API functions which will reset this value behind the scenes.
    Fortunately the App object has a "LastDLLError" member in VB5 which you can use instead.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • On-demand Event Event Date: October 23, 2014 Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this webcast to learn why physical appliances are preferable to virtual backup appliances, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds