如何使用 VBA(或 C#)将 MS Access 2007 中的链接表复制到本地表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17678767/
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
How to copy a linked table in MS Access 2007 to local table with VBA (or C#)?
提问by Jeff Brady
I have an Access 2007 database, c:\myDb.accdb
, with a table named MASTER_TABLE
that is linked to a table named TBL.MASTER_TABLE
in a DB2 database named PRODUCTION
.
我有一个 Access 2007 数据库,c:\myDb.accdb
其中一个名为的表MASTER_TABLE
链接到一个TBL.MASTER_TABLE
名为PRODUCTION
.
When I open MASTER_TABLE
, I'm prompted for my login credentials for the PRODUCTION
database.
当我打开 时MASTER_TABLE
,系统会提示我输入PRODUCTION
数据库的登录凭据。
I wrote an app in C# that uses the MASTER_TABLE
table, but of course it prompts the user for a login. To circumvent that, I need to copy the linked table to an existing local table named LOCAL_MASTER_TABLE
in myDb.accdb
.
我用 C# 编写了一个使用该MASTER_TABLE
表的应用程序,但它当然会提示用户进行登录。为了避免这种情况,我需要将链接表复制到名为LOCAL_MASTER_TABLE
in的现有本地表myDb.accdb
。
This copy needs to be automated, so the username/password needs to be passed programmatically instead of raising a prompt.
此副本需要自动化,因此需要以编程方式传递用户名/密码,而不是发出提示。
In Access, I tried the following to create a local copy, but it doesn't capture if a field is indexed:
在 Access 中,我尝试了以下方法来创建本地副本,但如果字段已编入索引,则不会捕获:
DoCmd.TransferDatabase acImport, "ODBC Database", "ODBC; DSN=PRODUCTION; UID=username; PWD=password; LANGUAGE=us_english; Database=PRODUCTION", acTable, "TBL.MASTER_TABLE", "LOCAL_MASTER_TABLE"
I also tried this: How to copy a linked table to a local table in Ms Access programmatically?
我也试过这个:如何以编程方式将链接表复制到 Ms Access 中的本地表?
But I get an error 3001: Arguements are of the wrong type, are out of acceptable range, or are in conflict with one another
但我收到一个错误 3001: Arguements are of the wrong type, are out of acceptable range, or are in conflict with one another
I'm calling the CopySchemaAndData_ADOX
function with the following:
我正在CopySchemaAndData_ADOX
使用以下命令调用该函数:
Call CopySchemaAndData_ADOX(TBL.MASTER_TABLE, LOCAL_MASTER_TABLE)
Call CopySchemaAndData_ADOX(TBL.MASTER_TABLE, LOCAL_MASTER_TABLE)
Anyway, can anyone think of a way to do this in either VBA or possibly in C#? Or why I'm getting the error in the code I tried to use?
无论如何,任何人都可以想出一种在 VBA 或 C# 中做到这一点的方法吗?或者为什么我在尝试使用的代码中遇到错误?
Thanks!
谢谢!
回答by HansUp
Use a "make table"query in an Access session.
在 Access 会话中使用“制作表”查询。
SELECT *
INTO LOCAL_MASTER_TABLE
FROM MASTER_TABLE;
You may need to provide your credentials when you execute that query.
执行该查询时,您可能需要提供凭据。
Since MASTER_TABLE
is a working linked table, LOCAL_MASTER_TABLE
will be created with Access-compatible field types.
由于MASTER_TABLE
是工作链接表,因此LOCAL_MASTER_TABLE
将使用与 Access 兼容的字段类型创建。
Turns out that suggestion is not appropriate because you want to automate the import and avoid asking the user for DB2 credentials each time.
事实证明,该建议并不合适,因为您希望自动导入并避免每次都向用户询问 DB2 凭据。
However you do have DoCmd.TransferDatabase
working except that it doesn't pull in the index. So I'll suggest you do the TransferDatabase
and then follow up by executing a CREATE INDEXstatement to get what you need.
但是,您确实可以DoCmd.TransferDatabase
工作,只是它不会拉入索引。因此,我建议您执行此操作TransferDatabase
,然后通过执行CREATE INDEX语句来获取您需要的内容。