Hello
This macro exports a column from Excel to Access.
I need to set it so that each time it exports, it clears the Access table's ROWS of all data before pasting. Column headers NEED to remain unchanged.
How can I do this?
Sub EXPORT_ADO_ExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Documents and Settings\HowAlex\Desktop\db1.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "TestEquities", 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("Security (ISIN)") = Range("A" & r).Value
.Fields("Designation") = Range("B" & r).Value
.Fields("BBG Format") = Range("C" & r).Value
.Fields("Security Name") = Range("D" & r).Value
.Fields("Asset Type") = Range("E" & r).Value
.Fields("Ticker") = Range("F" & r).Value
.Fields("Industry / Sector") = Range("G" & r).Value
.Fields("Last Price") = Range("H" & r).Value
.Fields("Current Market Cap") = Range("I" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
' The macro example assumes that your VBA project has added a reference to the ADO object library.
Thank you very much
Bookmarks