1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| Dim db As DAO.Database, errX As DAO.Error
Dim Wksp As DAO.Workspace, Conn As DAO.Connection, rsodbc As DAO.Recordset
Dim strOdbcLinkedTable As String, strODBCconn As String
Dim strSQL As String, strSELECT As String, i As Integer
Dim strErrMsg As String
strSELECT = "SELECT * FROM dbo.Fournisseurs"
Set db = CurrentDb
On Error GoTo ERRH
' Créer un nouveau workspace de type ODBC
Set Wksp = DBEngine.CreateWorkspace("Wksp2", "Admin", "", dbUseODBC)
' Chaîne de connexion ODBC
' * Copie la propriété Connect d'une table liée odbc
strOdbcLinkedTable = "dbo_Fournisseurs"
strODBCconn = db.TableDefs(strOdbcLinkedTable).Connect
' Ouverture connexion ODBC
Set Conn = Wksp.OpenConnection("sqlserver", dbDriverNoPrompt, False, strODBCconn)
For i = 10 To 20
' Source SQL de la requête temporaire
strSQL = strSELECT & " WHERE ([N° fournisseur]=" & i & ")"
' Ouvrir recordset à partir du résultat de la requête
Set rsodbc = Conn.OpenRecordset(strSQL, dbOpenSnapshot, dbExecDirect)
If Not rsodbc.EOF Then Debug.Print rsodbc("N° fournisseur"); " "; rsodbc("Société")
rsodbc.Close
Next
QUIT:
Set db = Nothing
If Not rsodbc Is Nothing Then rsodbc.Close
If Not Conn Is Nothing Then Conn.Close
If Not Wksp Is Nothing Then Wksp.Close
Exit Sub
ERRH:
strErrMsg = "Erreur N° " & CStr(Err.Number) & " : " & Err.Description
Select Case Err.Number
' Erreurs ODBC
Case 3146, 3151, 3154, 3155, 3156, 3157, 3231, 3232, 3234, 3225, 3238, 3247, 3254
strErrMsg = strErrMsg & vbCrLf & vbCrLf & _
">>> Erreurs complémentaires DAO :" & vbCrLf & _
"======================"
' Récupérations Erreur(s) driver ODBC
For Each errX In DBEngine.Errors
strErrMsg = strErrMsg & vbCrLf & Format(errX.Number, "00000") & " : " & errX.Description
Next
' L'objet est incorrect ou n'est plus défini.
' Survient si on exécute plus d'une fois Object.Close
Case 3420
Resume Next
End Select
MsgBox strErrMsg
Resume QUIT |