Updating Table in Access from Excel using DAO - TechRepublic
Question
June 11, 2009 at 07:46 AM
progof

Updating Table in Access from Excel using DAO

by progof . Updated 17 years, 1 month ago

Hi there, I am currently exporting data from a set of defined columns in an Excel spreadsheet into an Access table using DAO. It works fine, but the problem is that for a specific month e.g. April I upload it once, but when there is a change and I upload it again, I need to delete the old data (without affecting the data from other previous months). This is the code that I have at the moment:

Dim db As Database, rs As Recordset, ws As Worksheet, r As Long

Set db = OpenDatabase(“C:\BS Sales.mdb”)
Set rs = db.OpenRecordset(“CMZ_1_PL_EXPENSE”, dbOpenTable)
Set ws = ThisWorkbook.Worksheets(“Output”)
r = 2
Do While Len(ws.Range(“A” & r).Formula) > 0
With rs
.AddNew

.Fields(“Month”) = ws.Range(“A” & r).Value
.Fields(“area”) = ws.Range(“B” & r).Value
.Fields(“code”) = ws.Range(“C” & r).Value
End With
r = r + 1 ‘ next row
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

I would like to add a condition that will allow to delete from the table the existing records whose “Date” is the same as the ones I am trying to add.

Many thanks for your time and help

This discussion is locked

All Comments