vba 将表导出到另一个访问数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12813865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:03:24  来源:igfitidea点击:

Export table to another access database

vbams-accessms-access-2007

提问by Jofsey

I have a query in my Access database and a table with absolutely the same structure in another Access database. I'm need to write a script which adds all entries from the query to the table. How can I do this?

我的 Access 数据库中有一个查询,另一个 Access 数据库中有一个结构完全相同的表。我需要编写一个脚本,将查询中的所有条目添加到表中。我怎样才能做到这一点?

The difference between my task and functionality of Extarnal Data -> Export -> Access Databaseis that I need to addnew entries and save the old, but this tool can only replaceold entries to new.

我的任务和功能之间的区别在于我Extarnal Data -> Export -> Access Database需要添加新条目并保存旧条目,但此工具只能旧条目替换为新条目。

回答by Zev Spitz

  1. Create a linked table in the source database, to the destination table.
  2. Then, use an append query to insert the results from your query into the linked table.
  1. 在源数据库中创建一个链接表,到目标表。
  2. 然后,使用追加查询将查询结果插入到链接表中。

Edit: You can also do this with a single SQL statement:

编辑:您也可以使用单个 SQL 语句执行此操作:

INSERT INTO DestinationTable (Field1, Field2)
IN "C:\path\to\file.accdb"
SELECT Field1,Field2
FROM SourceTable

But the referencesays:

但是参考文献说:

For improved performance and ease of use, use a linked table instead of IN.

为了提高性能和易用性,请使用链接表而不是 IN。

回答by Simon1979

For anyone looking to link the table in VBA...

对于任何希望在 VBA 中链接表格的人...

strDbName = "C:\FolderPath\DatabaseName.mdb"
strLinkTbl = "tblNameOfTableYouWantToLink"
strNameTbl = "tblWhatYouWantToNameIt" ' This can be the actual 
                                      ' table name or something different

DoCmd.TransferDatabase acLink, "Microsoft Access", strDbName, _
    acTable, strLinkTbl, strNameTbl

Then your SQL looks like:

然后你的 SQL 看起来像:

CurrentDb.Execute "INSERT INTO " & strNameTbl _
   & " (Field1, Field) SELECT Field1, Field2 FROM SourceTable