I'm trying to fill a table in access from a table in Excel. The code is enbeded in an Excel worksheet.
I'm using the following code:
The problem is that using .AddNew, I'm just adding a new entry and it won't just replace prior entries. I thought of two solutions. Either deleting the whole entries of the table and the adding the new entries, or replacing every entry one by one. How could I solve this?Code:Sub Export() Dim db As Database Dim rs As Recordset Dim col, row, field_count, i As Integer Dim MyField, tabla As String Set db = OpenDatabase("C:\Base.mdb") table = "TABLE" Set rs = db.OpenRecordset(table, dbOpenTable) row = 2 Do While Len(Range("A" & row).Formula) > 0 With rs field_count = 5 .AddNew For i = 1 To field_count MyField= Worksheets("FIELDS").Cells(i + 3, 1).Value col= Worksheets("FIELDS").Cells(i + 3, 3).Value .Fields(MyField) = Worksheets(table).Cells(row, col).Value Next i .Update End With row= row+ 1 Loop rs.Close Set rs = Nothing db.Close Set db = Nothing End Sub




Reply With Quote