Sources

 

Connection à une base Access et modèle de traitement des erreurs

Private Sub cmdTemplate_Click()

Dim Conn1 As Connection
Dim Errs1 As Errors
Dim i As Integer
Dim StrTmp
On Error GoTo AdoError
Set Conn1 = CreateObject("ADODB.Connection")

' ouvrir une connection à une fausse source ODBC pour BIBLIO.MDB
Conn1.ConnectionString = "DBQ=BIBLIO.MDB;" & _
"DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DefaultDir=C:\Bogus\Directory\Path;" & _
"UID=admin;PWD=;"


Conn1.Open

' Votre code ICI
Done:

' Fermer la connection
Conn1.Close

' Detruire la connection
Conn1 = Nothing
' Sortir
Exit Sub

AdoError:
Dim errLoop As Error
Dim strError As String
' In case Conn1 isn't set or other initialization problems
On Error Resume Next
i = 1

' Traiter les erreurs
StrTmp = StrTmp & vbCrLf & "VB Error # " & Str(Err.Number)
StrTmp = StrTmp & vbCrLf & " Generated by " & Err.Source
StrTmp = StrTmp & vbCrLf & " Description " & Err.Description

' Passer en revue la collection Errors et afficher la propriétés de chaque objet en erreur
Set Errs1 = Conn1.Errors

For Each errLoop In Errs1

StrTmp = StrTmp & vbCrLf & "Error #" & i & ":"
StrTmp = StrTmp & vbCrLf & " ADO Error #" & errLoop .Number
StrTmp = StrTmp & vbCrLf & " Description " & errLoop.Description
StrTmp = StrTmp & vbCrLf & " Source " & errLoop.Source
i = i + 1

Next

MsgBox StrTmp
On Error Resume Next
GoTo Done
End Sub