Error handling refers to the practice of advance programming and coding for error conditions that can arise when the program runs. We are just avoiding the error. In VBA we are using On Error statement for handling. This statement instructs VBA what action will perform on error. Error handler will enable when on error statement is executed. To check the cause of the error in VBA, we need to check the value of the number property of the VBA Err object. The description property of the Err object contains the message for the encountered error number. In ADO, using VBA Error object we can get the information about the error. Error object is fetching the information form the ADO error collection. Using description property of error objects we can show the details of the error.
In this article we are use he VBA error object and ADO error collection to get information about error. To implement this we have to write code for fetch error information. After implementation we have to debug this code and the message will prompt and show open immediate window as shown in Fig 1.1.
The detail of error will show in immediate window as shown in fig 1.2.
Option Compare Database Sub DBError() Dim con As New ADODB.Connection Dim ErrorADO As ADODB.Error On Error GoTo ChkErrors con.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=D:\test.mdb" ChkErrors: Debug.Print "Error number: " & Err.Number & vbCrLf & " (" & Err.Description & ")" Debug.Print "Information regarding this error " & vbCrLf & "contained in the ADO Errors collection." For Each ErrorADO In con.Errors Debug.Print vbTab & "Error Number: " & ErrorADO.Number Debug.Print vbTab & "Error Description: " & ErrorADO.Description Debug.Print vbTab & "Jet Error Number: " & ErrorADO.SQLState Debug.Print vbTab & "Native Error Number: " & ErrorADO.NativeError Debug.Print vbTab & "Source: " & ErrorADO.Source Debug.Print vbTab & "Help Context: " & ErrorADO.HelpContext MsgBox ("Error detail on immediate window") Next End Sub
It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. ERP Makers will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.