» 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
If you want to export data to an Access table from an Excel worksheet, the macro example below shows how this can be done:
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:
- Managing by the Numbers: A Commonsense Guide to Understanding and Using Your Company's Financials: An Essential Resource for Growing Businesses
- Financial Shenanigans : How to Detect Accounting Gimmicks & Fraud in Financial Reports
- Infectious Greed: How Deceit and Risk Corrupted the Financial Markets
- Harry Potter and the Order of the Phoenix (Book 5)
- The Accounting Game : Basic Accounting Fresh from the Lemonade Stand
No comments have been submitted.

