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
| Sub RetrieveDatas()
Dim Rows
ClearTable
Rows = getRows()
Rows = Transpose(Rows)
If Not IsEmpty(Rows) Then Range("t_Contacts").Resize(UBound(Rows) + 1).Value = Rows
End Sub
Sub ClearTable()
If Not Range("t_Contacts").ListObject.DataBodyRange Is Nothing Then _
Range("t_Contacts").ListObject.DataBodyRange.Delete
End Sub
Function getRows()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim FileName As String
Dim Rows
FileName = "D:\Documents\Database29.accdb"
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & FileName
Set rs = cn.Execute("select contactpk, firstname, lastname from contact")
If Not rs.EOF Then getRows = rs.getRows()
rs.Close
cn.Close
End Function
Function Transpose(Rows)
Dim i As Long, j As Long
ReDim t(UBound(Rows, 2), UBound(Rows))
For i = 0 To UBound(Rows)
For j = 0 To UBound(Rows, 2)
t(j, i) = Rows(i, j)
Next
Next
Transpose = t
End Function |
Partager