» Export data from Excel to Access (DAO) using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Import and Export in VBA
VERSION - All Microsoft Excel Versions
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 Database, rs As Recordset, r As Long
Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb")
' open the database
Set rs = db.OpenRecordset("TableName", 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...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
The macro example assumes that your VBA project has added a reference to the DAO object library.You can do this from within the VBE by selecting the menu Tools, References and selecting Microsoft DAO x.xx Object Library.
Use ADO if you can choose between ADO and DAO for data import or export.
Book Store:
Recommended Books:
- MP Managerial Accounting w/ Topic Tackler, Net Tutor, & PowerWeb
- Investments + S&P Card + Powerweb + StockTrak discount coupon
- The One Page Business Plan: Start With a Vision, Build a Company!
- Microsoft Windows XP Inside Out
- Windows XP Pocket Reference
- Analyzing Markets, Products, and Marketing Plans
No comments have been submitted.

