Detecting a specific error using OLE DB Provider for Jet


I am trying to detect a specfic error when adding a new record using the .AddNew method.

The table in question is from SQL Server, linked to an Access '97 Database - hence the OLD DB Provider.

My problem is that I cannot seem to extract a specific error code - in this instance a 3022 (duplicate record).

I have tried the standard VB error object and the ADO errors collection, but all I get is -2147217887 (error detected, big help).

I assume there must be an API call I must use or something but cannot figure out what it is.

Any help would be appreciated.


Pete Brassett
Who is Participating?
You say you linked a SQL Table into AccessDB then use the Jet-Ole-db-Provider ( from a VB app? ).
Problem is not OLEDB, but Accessing a remoteDB via Jet.
This hides the Actual Error, you'll get the Access ( JET ) interpretation of the OLEDB Error. Why can't you just connect directly to SQL via SQL-Driver (and If needed to Access via Jetdriver)from VB, this gives you the Error Raised in SQL-Server, not what Jet thinks SQL-Error was.
In my experience, ADO is notoriously undescriptive about it's errors, cf. the well-known but useless "Errors Occured" message.

Have you tried examining the NativeError property of the ADODB Error object? This property will sometimes (...) return the original error ID as raised by the provider, e.g. the 3022 you mentioned above. No guarantees though.


Éric MoreauSenior .Net ConsultantCommented:
Loop the Errors collection of the Connection object.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pbrassetAuthor Commented:
The NativeError property just returns a 3146 (ODBC Error - again, big help).

I have tried looping thru the errors collection which doesn't provide any more info.
I assume the "SQLState" property of the ADO error object doesn't return anything useful either?

Try this:

   On Error Goto ErrHandler:

   ... your code ...

   If Err.Number = 3146 Then ' ODBC Call failed
       MsgBox Error$
   End If

According to the Knowledge Base article Q120763, this should (...) return the original error description, followed by the original error number between parentheses.

pbrassetAuthor Commented:
SQLState gives the 3146, which doesn't help.

The err.number returned is -2147217887, while the error$ (and err.description) gives ODBC--call failed.
As a last resort, you could try to wrap the insert in a SQL-Server stored procedure, catching any errors there.

Other than that, I suspect you're out of options ;-(
Hi pbrasset@devx,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept niklausj's comment(s) as an answer.

pbrasset@devx, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with my recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Comment from expert accepted as answer

E-E Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.