Class ODBCError (Excel VBA)

The class ODBCError represents an ODBC error generated by the most recent ODBC query.

Set

To use a ODBCError class variable it first needs to be instantiated, for example

Dim lngIndex As Long: lngIndex = 
Dim odberODBCError As ODBCError
Set odberODBCError = Application.ODBCErrors(Index:=lngIndex)

The following procedures can be used to set variables of type ODBCError: ODBCErrors.Item and Application.ODBCErrors

For Each

Here is an example of processing the ODBCError items in a collection.

Dim odberODBCError As ODBCError
For Each odberODBCError In Application.ODBCErrors
	
Next odberODBCError

ErrorString

Returns a String value that represents the ODBC error string.

Parent

Returns the parent object for the specified object. Read-only.

SqlState

Returns the SQL state error.

For an explanation of the specific error, see your SQL documentation.

With Worksheets(1).QueryTables(1) 
 .Refresh 
 Set errs = Application.ODBCErrors 
 If errs.Count > 0 Then 
 Set r = .Destination.Cells(1) 
 r.Value = "The following errors occurred:" 
 c = 0 
 For Each er In errs 
 c = c + 1 
 r.offset(c, 0).value = er.ErrorString 
 r.offset(c, 1).value = er.SqlState 
 Next 
 Else 
 MsgBox "Query complete: all records returned." 
 End If 
End With

ODBCErrors.Count

Returns a Long value that represents the number of objects in the collection.

ODBCErrors.Item

Returns a single object from a collection.

Syntax : expression.Item (Index)

Index: The index number for the object.

Set er = Application.ODBCErrors.Item(1) 
MsgBox "The following error occurred:" & 
 er.ErrorString & " : " & er.SqlState

ODBCErrors.Parent

Returns the parent object for the specified object. Read-only.