I am trying to figure out a way to export any new data entered into an Excel 2003 spreadsheet into an Access 2003 database from the Excel application. I've seen lots of code originating in the Access application, but I want to avoid having to go into Access to do it. The users will be entering data into Excel daily, but no one will be in Access daily- and the info needs to be up to date because it is a source for another application(an Infopath 2003 form)which will also be used daily.
Only the newly entered data would need to be copied, and the primary key would not be in the Excel sheet.
I'm a newbie to VBA,
my code which dont work.....
Only the newly entered data would need to be copied, and the primary key would not be in the Excel sheet.
I'm a newbie to VBA,
my code which dont work.....
Code:
Sub DAOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim db As DAO.Database, rs As DAO.Recordset, r As Long
Dim rs2 As DAO.Recordset, lngPK As Long
' open the database
Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb")
' open first recordset
Set rs = db.OpenRecordset("TableName", dbOpenTable)
' open second recordset
Set rs2 = db.OpenRecordset("OtherTableName", dbOpenTable)
' get all records in a table
r = 3 ' 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("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' add more fields if necessary...
' get the primary key
lngPK = .Fields("AutoNumField")
.Update ' stores the new record
End With
With rs2
' do something with the other recordset here
' you can use lngPK
End With
r = r + 1 ' next row
Loop
rs2.Close
Set rs2 = Nothing
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
Comment