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
| Sub ADOFromExcelToAccess()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Chemin = ActiveWorkbook.Path
Set db = OpenDatabase("chemin database.mdb")
Source = Chemin & "\database name"
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= " & Source & ";"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "[MA TABLE ACCESS]", cn, adOpenKeyset, adLockOptimistic, adCmdTable ' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0 ' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("COL1") = Range("A" & r).Value
.Fields("COL2") = Range("B" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub |
Partager