Class ODBCError (Excel VBA)

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

Item

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

Dim odber as ODBCError
Set odber = Application.ODBCErrors(Index:=1)

ODBCError class variables can be set using the ODBCErrors.Item procedure.

For Each

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

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

ErrorString

Returns a String value that represents the ODBC error string.

Dim strErrorString As String
strErrorString = Application.ODBCErrors(1).ErrorString

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.

Dim lngCount As Long
lngCount = Application.ODBCErrors.Count

ODBCErrors.Item

Returns a single object from a collection.

Item (Index)

Index: The index number for the object.