0
我有一個MS Access數據庫與一個鏈接表指向一個SQL Server實例通過ODBC。如果我嘗試插入違反外鍵約束的行,則會得到一個相當難看的錯誤消息:「ODBC - 調用失敗」,並提供進一步的技術細節。「ODBC調用失敗」錯誤消息是否可以被捕獲並顯示爲友好的錯誤消息?
我可以攔截此錯誤消息並向用戶提供更友好的解釋嗎?
我有一個MS Access數據庫與一個鏈接表指向一個SQL Server實例通過ODBC。如果我嘗試插入違反外鍵約束的行,則會得到一個相當難看的錯誤消息:「ODBC - 調用失敗」,並提供進一步的技術細節。「ODBC調用失敗」錯誤消息是否可以被捕獲並顯示爲友好的錯誤消息?
我可以攔截此錯誤消息並向用戶提供更友好的解釋嗎?
你可以把錯誤在表單級別處理,像這樣:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conDuplicateKey = 3146
Dim strErrMsg As String
Select Case DataErr
Case conDuplicateKey:
strErrMsg = "Record already exists in the database!"
' TODO: add other error codes
Case Else
Response = acDataErrDisplay
End Select
If strErrMsg <> vbNullString Then
MsgBox strErrMsg, vbCritical, Me.Caption
ActiveControl.Undo
Response = acDataErrContinue
End If
End Sub