Handling Errors from the Server

Whenever your application causes an error to occur on the server, Microsoft Access displays two messages: first the Microsoft Access error message and then the message from the server. If you trap an ODBC error (numbered 3146 through 3299) in a Visual Basic procedure, the Err object returns both the Microsoft Access message and all server error messages concatenated together. The value returned by the Err object tells you which server error occurred as well as the text of the server error message.

The following error-handling code demonstrates how you can obtain the server error number and message when an ODBC error occurs.

Dim intRemoteError As Integer, strErrorText As String
Dim intNumStart As Integer, intTextStart As Integer

Select Case Err
	Case 3146 To 3299			' It's an ODBC error, so parse it.
		' Find error number.
		intNumStart = InStr(Err, "(#") + 2
		intRemoteError = Val(Mid$(Err, intNumStart))

		' Find error message.
		intTextStart = InStr(Err, "] ") + 2
		strErrorText = Mid$(Err, intTextStart, _
			intNumStart - intTextStart - 3)
		.
		. ' Handle remote error.
		.
	Case Else
		.
		. ' Handle other errors.
		.
End Select

See Also   For more information on errors, search the Help index for “errors.”