un peu mieux
Option Explicit
'Cocher Outils References ActiveX Data Objects x.x Object Library.
Public Sub ADOImportTableAccess(FichierBaseAccess As String, NomTable As String, Cible As Range)
Dim Connexion As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim i As Integer
Dim Requete As String
Set Connexion = New ADODB.Connection
Connexion.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & FichierBaseAccess & ";"
Set Rs = New ADODB.Recordset
Requete = "SELECT NOM, PRENOM, AILE, CHAMBRE, UNITE FROM " & NomTable & " WHERE (PRESENT = -1) AND (DISPONIBLE = 0) ORDER BY UNITE ASC,NOM ASC"
With Rs
.CursorLocation = adUseClient
.Open Requete, Connexion, adOpenDynamic, adLockOptimistic
For i = 0 To Rs.Fields.Count - 1
Cible.Offset(0, i).Value = Rs.Fields(i).Name
Next
Cible.Offset(1, 0).CopyFromRecordset Rs
End With
Rs.Close
Set Rs = Nothing
Connexion.Close
Set Connexion = Nothing
End Sub
Sub Bouton1_QuandClic()
Dim Chemin As String
Dim NomTable As String
Dim Cible As Range
Application.ScreenUpdating = False
Cells.Clear
Chemin = ThisWorkbook.Path & "\Cpasan.mdb"
NomTable = "CPASAN"
Set Cible = Cells(1, 1)
ADOImportTableAccess Chemin, NomTable, Cible
Cells.Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1" ).Select
Application.ScreenUpdating = True
End Sub
Message édité par kiki29 le 24-05-2006 à 12:45:25