Hi AG,
I have sent an email to you regarding this issue. Please check it.
To share it with the community, I extract part of the content and post it
here:
=============================================================
The question you asked is actually a by design limitation in Access, no
matter in 2007 or its earlier version.
An ODBC error message is composed with two parts:
<Part 1>ODBC call failed.
<Part 2> [Microsoft][ODBC SQL Server Driver][SQL Server] <Server-specific
error message>
By design the first part can be trapped by the code specified in the
OnError property for a form, however the second part cannot. As you might
have seen, no matter what you had written in the OnError procedure, the
second part of error message box always appeared on the screen after the
event procedure code finished running. There is an old KB article
dedicatedly addressing this issue:
ACC: Cannot Trap ODBC Errors with Form OnError Property Code
http://support.microsoft.com/kb/124395/en-us
If we look at the RESOLUTION part of the document, we can see the
following
description:
"If the ODBC error is triggered by a procedure that contains a routine to
handle errors (rather than by a form), the procedure will be able to trap
both parts of the error message."
In other words, if you want to trap the second part of the error message,
you need to avoid using bound form, instead write your custom code to
query
and update data in a procedure and trap the error message in the
procedure.
I write the following test code for your reference:
------------------------------------------------
Private Sub Form_Load()
On Error GoTo Error_Trap
Dim strErr As String
Dim intErr As Integer
Dim mydb As Database
Dim myq As QueryDef
Set mydb = CurrentDb()
Set myq = mydb.CreateQueryDef("")
myq.Connect =
"ODBC;DSN=my2k5;UID=;PWD=;LANGUAGE=us_english;DATABASE=Northwind"
myq.ReturnsRecords = False
' Any SQL statement will work below.
myq.SQL = "update dbo.customers set companyname='Ernst Handel' where
customerid='EASTC' "
myq.Execute
Exit Sub
Error_Trap:
'strErr = "Access error " & CStr(DataErr) & " " & AccessError(DataErr)
If DBEngine.Errors.Count > 0 Then
strErr = strErr & vbCrLf & "DbEngineErrors:"
For intErr = 0 To DBEngine.Errors.Count - 1
strErr = strErr & vbCrLf & DBEngine.Errors(intErr).Number & " / "
& DBEngine.Errors(intErr).Description & " / " &
DBEngine.Errors(intErr).Source
Next
End If
MsgBox strErr, vbCritical, "Not What Is Desired"
End Sub
----------------------------------------------------------------------
Not sure why the KB article is not updated to apply to Access 2003 and
2007
yet. I have submitted a feedback to our document team for this.
=====================================================
Any further questions or concerns are welcomed!
Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
=========================================================