tallgeese980
Board Regular
- Joined
- Nov 19, 2009
- Messages
- 80
The code below works great at adding data to a database, however if the unique identifier already exists it will not modify the table but instead just append. So if column 2 has April and I find out the cost data for April has changed this code will just add more rows for April as opposed to updating the April data already in the database. I tried changing the oRS.AddNew to oRS.Edit but that did not work. Any suggestions?
Sub ExportSummaryToAccess()
Dim oSelect As Range, i As Long, j As Integer, sPath As String
Set oSelect = Sheets("Summary Upload").Range("A2:H9")
Dim oDAO As DAO.DBEngine, oDB As DAO.Database, oRS As DAO.Recordset
ChDir ActiveWorkbook.Path
sPath = "F:\USERDATA\DB Project\Test.accdb"
If sPath = "False" Then Exit Sub
Set oDAO = New DAO.DBEngine
Set oDB = oDAO.OpenDatabase(sPath)
Set oRS = oDB.OpenRecordset("SUMMARYCOSTDATA")
For i = 1 To oSelect.Rows.Count
oRS.AddNew
For j = 1 To oSelect.Columns.Count
oRS.Fields(j) = oSelect.Cells(i, j)
Next j
oRS.Update
Next i
oDB.Close
End Sub
Sub ExportSummaryToAccess()
Dim oSelect As Range, i As Long, j As Integer, sPath As String
Set oSelect = Sheets("Summary Upload").Range("A2:H9")
Dim oDAO As DAO.DBEngine, oDB As DAO.Database, oRS As DAO.Recordset
ChDir ActiveWorkbook.Path
sPath = "F:\USERDATA\DB Project\Test.accdb"
If sPath = "False" Then Exit Sub
Set oDAO = New DAO.DBEngine
Set oDB = oDAO.OpenDatabase(sPath)
Set oRS = oDB.OpenRecordset("SUMMARYCOSTDATA")
For i = 1 To oSelect.Rows.Count
oRS.AddNew
For j = 1 To oSelect.Columns.Count
oRS.Fields(j) = oSelect.Cells(i, j)
Next j
oRS.Update
Next i
oDB.Close
End Sub