该范例触发并捕获错误,同时显示产生的 Error 对象的 Description、HelpContext、HelpFile、NativeError、Number、Source 和 SQLState 属性。
Public Sub DescriptionX() Dim cnn1 As ADODB.Connection Dim errLoop As ADODB.Error Dim strError As String On Error GoTo ErrorHandler ' 有意触发错误。 Set cnn1 = New ADODB.Connection cnn1.Open "nothing" Exit SubErrorHandler: ' 枚举错误集合并显示每个 Error 对象的属性。 For Each errLoop In cnn1.Errors strError = "Error #" & errLoop.Number & vbCr & _ " " & errLoop.Description & vbCr & _ " (Source: " & errLoop.Source & ")" & vbCr & _ " (SQL State: " & errLoop.SQLState & ")" & vbCr & _ " (NativeError: " & errLoop.NativeError & ")" & vbCr If errLoop.HelpFile = "" Then strError = strError & _ " No Help file available" & _ vbCr & vbCr Else strError = strError & _ " (HelpFile: " & errLoop.HelpFile & ")" & vbCr & _ " (HelpContext: " & errLoop.HelpContext & ")" & _ vbCr & vbCr End If Debug.Print strError Next Resume NextEnd Sub