Excel VBA import data to existing Access database

时间秒杀一切 提交于 2020-01-03 05:41:06

问题


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!


回答1:


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.




回答2:


Have you included references in VBA for Access?

Tools > references > Microsoft Access xx.x Object Library


来源:https://stackoverflow.com/questions/21081014/excel-vba-import-data-to-existing-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!