简体   繁体   English

是否可以在 Excel 中使用 VBA 将 Excel 数据附加到 Access 数据库文件?

[英]Is it possible to append Excel data to an Access database file using VBA within Excel?

I have three cells in an in Excel 2010 worksheet, say a1, a2, and a3.我在 Excel 2010 工作表中有三个单元格,比如 a1、a2 和 a3。 Every time the user runs my Excel macro, I need it to take the info in those cells and append it to an existing Access DB file.每次用户运行我的 Excel 宏时,我都需要它获取这些单元格中的信息并将其附加到现有的 Access DB 文件中。 That is all that will be in the db file, just a running list.这就是 db 文件中的全部内容,只是一个运行列表。

So, I don't want to IMPORT from Access.所以,我不想从 Access 导入。 I want this all to happen on the Excel side, preferably without opening access at all.我希望这一切都发生在 Excel 端,最好根本不开放访问。 Is this possible or can I just tell my husband to forget about it?这是可能的还是我可以告诉我丈夫忘记它?

If it IS possible, can someone give me a clue as to how to go about it?如果可能的话,有人能给我一个关于如何去做的线索吗? Or where to learn about it?或者去哪里学习? I'm ok with VBA in Excel but have zero experience with Access or even with databases.我对 Excel 中的 VBA 没问题,但对 Access 甚至数据库的经验为零。

Thanks!谢谢!

Create a reference to the Microsoft DAO 3.6 object library and start playing with this code:创建对Microsoft DAO 3.6 对象库的引用并开始使用以下代码:

Sub DBInsert()
Dim DB As DAO.Database
Dim RS As DAO.Recordset

    ' open database
    Set DB = DAO.OpenDatabase("C:\Users\Myself\Desktop\MyDB.mdb")

    ' open table as a recordset
    Set RS = DB.OpenRecordset("Table1")

    ' add a record to the recordset
    RS.AddNew

    ' fill fields with data ... in this case from cell A1
    RS.Fields("Field1") = [A1]

    ' write back recordset to database
    RS.Update

    ' important! cleanup
    RS.Close

    ' forget to close the DB will leave the LDB lock file on the disk
    DB.Close

    Set RS = Nothing
    Set DB = Nothing
End Sub

Create a button on the sheet and place this code inside the Button_Click() so the user can send the data to your DB when all entry is done.在工作表上创建一个按钮并将此代码放在 Button_Click() 中,以便用户可以在完成所有输入后将数据发送到您的数据库。

Further resources:更多资源:

Office 2013 / Data Access / How do I ... Office 2013 / 数据访问 / 我如何...

Choosing ADO or DAO for Working with Access Databases 选择 ADO 或 DAO 来使用 Access 数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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