简体   繁体   中英

Excel VBA import data to existing Access database

I've found similar questions asked before, but I can't seem to figure it out using those examples.

I'm writing a macro in Excel where the last step needs to import all the data in Sheet1!A:Q (headers in row 1) to Table1 in an Access database. I need to make sure each column in Excel corresponds to the correct field in Access.

I am using Excel 2010/2013 and Access 2010/2013. Code I am currently using is:

Sub AccImport()

Dim acc As New Access.Application
acc.OpenCurrentDatabase "C:\Users\Benjamin\Documents\Database1.accdb"
acc.DoCmd.TransferSpreadsheet _
        acImport, _
        acSpreadsheetTypeExcel12Xml, _
        "Sheet1", _
        Application.ActiveWorkbook.FullName, _
        True, _
        "A1:Q1000"         'Need to change to reflect the last row in the sheet
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing

End Sub

The code results in error message, highlighting acc As New Access.Application and displaying "Compile error - User-defined type not defined"

Any help would be greatly appreciated. Thanks!

Try this: In the VBA Editor, go to Tools -> References. Scroll down to "Microsoft Access 11.0 Object Library" (yours might not be 11.0, but you're looking for the one that says Microsoft Access) and ensure the box is checked. If not, check it and click OK. This solved the compile error when I tested it.

您是否在VBA for Access中包含了参考?

Tools > references > Microsoft Access xx.x Object Library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM