ExcelTip.com
ExcelTip.com
Account Icon Account Icon Account Icon
Google Exceltip.com
JOIN OUR NEWSLETTER
  and receive for joining:
Free eBook Learn More!
Free Excel ADD-IN
Free Weekly Excel Tip
4 e-books in cd-rom
F1 Formulas & Functions
F1 Formulas & Functions
F1 EXCEL
F1 eBook (Spanish)
F1 EXCEL
Financial Statements.xls
 

» Export data from Excel to Access (ADO) using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions


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 ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=C:\FolderName\DataBaseName.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable  
    ' 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
    cn.Close
    Set cn = Nothing
End Sub
The macro example assumes that your VBA project has added a reference to the ADO object library.
You can do this from within the VBE by selecting the menu Tools, References and selecting Microsoft
ActiveX Data Objects x.x Object Library.
Use ADO if you can choose between ADO and DAO for data import or export.
Rate this tip
12 34 5
  RATING: 3.74
  VIEWS: 92732

READER COMMENTS (view all comments)


Excel to Access export in VBA
Lwangaman wrote on December 31, 1969 19:00 EST
Just wanted to ask how I can do this with a Microsoft Access database that I've protected with a workgroup, usernames and passwords in an .mdw file. I need a specific line of code to enter my username and password in order to be able to connect to the database and to the table...
EXPORT/IMPORT
DARLENE wrote on December 31, 1969 19:00 EST
HOW DO I EXPORT FROM EXCEL TO MS DOS--IT KEEPS TELLING ME TO SAVE EACH SHEET INDIVIDUALLY, NO MATTER WHICH OPTION I CHOOSE. HOW DO I SAVE ALL THE PAGES---NOT INDIVIDUALLY



REGISTERED USERS - Click here to post comments


GUESTS (Click here to register)
Name
Comment Title
Comments

Advanced modelling in finance using Excel and VBA

Keys to Reading an Annual Report (Barron's Business Keys)

Business Plans Kit for Dummies (With CD-ROM)

How to Pay Zero Taxes (Annual)

The One Page Business Plan: Start With a Vision, Build a Company!

The Analysis and Use of Financial Statements

RELATED MICROSOFT EXCEL TIPS


Convert PDF Files to Excel


Excel VBA books
Accounting books
Business Plans
MS Office books
Taxes books

VIEW ALL BOOKS