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
|
Sub connexionbaseAccess(Fichier As String)
Set CnAccess = New ADODB.Connection
With CnAccess
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & Fichier
.Open
End With
Set RstAccess = New ADODB.Recordset
End Sub
Sub Test()
Dim requete As String
Dim NomFeuille As String
Dim feuille As Excel.Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
requete = "Select t1.`Ordre` From ImportIW49N as t1 Where t1.`Ordre` Not In (Select distinct t2.Ordre from ImportIW49N as t2 Where t2.StatutOP Like '%CONF%' Or t2.StatutOP Like '%CNFP%')"
NomFeuille = "Sans_Aucune_Confirmation"
'RAZ de la feuille
If SheetExists(NomFeuille) Then
ThisWorkbook.Sheets(NomFeuille).Delete
End If
ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)).Name = NomFeuille
Set feuille = ThisWorkbook.Sheets(NomFeuille)
With feuille
Call connexionbaseAccess(PathBase)
RstAccess.Open requete, CnAccess, adOpenStatic, adLockOptimistic
For i = 0 To RstAccess.Fields.Count - 1
.Cells(1, i + 1) = RstAccess.Fields(i).Name
Next i
If RstAccess.RecordCount > 0 Then
[A2].CopyFromRecordset RstAccess
End If
End With
Call DeconnexionBaseAccess
Set feuille = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub |
Partager