简体   繁体   English

如何使用 VBA 创建 CurrentDb 的副本

[英]How do I create copies of CurrentDb using VBA

I need to create copies of the CurrentDB using VBA (approx. 12 copies).我需要使用 VBA 创建 CurrentDB 的副本(大约 12 个副本)。 The copies need to be clones of the master database containing all the same forms, queries, etc. except only a limited dataset.副本需要是包含所有相同表单、查询等的主数据库的克隆,除了有限的数据集。

DoCmd.CopyDatabaseFile seems to be made for this, but only works if you are using it to copy the DB to an MS SQL Server. DoCmd.CopyDatabaseFile似乎是为此而制作的,但仅当您使用它将数据库复制到 MS SQL Server 时才有效。 As MS states on their website:正如 MS 在其网站上所述:

Copies the database connected to the current project to a Microsoft SQL Server database file for export.将连接到当前项目的数据库复制到 Microsoft SQL Server 数据库文件以进行导出。

docmd.TransferDatabase only exports the data itself, but not the structure, forms, etc. docmd.TransferDatabase只导出数据本身,不导出结构、表单等。

Code I have found on the web and adapted doesn't work and throws an error on the .CopyFile line saying:我在网上找到并改编的代码不起作用,并在.CopyFile行上抛出一个错误说:

Run-time error 52: Bad file name or number运行时错误 52:错误的文件名或编号

This is the code这是代码

Sub CopyDB()
Dim external_db As Object
Dim sDBsource As String
Dim sDBdest As String

sDBsource = "\\group\bsc\groups\L\BDTP\Compliance\ComplianceData\Compliance Group Reporting.accdb"
sDBdest = "\\group\bsc\groups\L\BDTP\Compliance\ComplianceData\Compliance Group Reporting_2.accdb"""

Set external_db = CreateObject("Scripting.FileSystemObject")
external_db.CopyFile sDBsource, sDBdest, True
Set external_db = Nothing

End Sub

How can I fix the above line?我怎样才能解决上面的问题? Alternatively is there a direct command in Access to create a copy?或者,Access 中是否有直接命令来创建副本? The "create backup" function would be tailor made for this, but I can not find it in VBA. “创建备份”功能将为此量身定制,但我在 VBA 中找不到它。

Looks like you have an extra quote in sDBdest accdb""" And for database copy you can also use看起来您在 sDBdest accdb"""有一个额外的引号,对于数据库副本,您也可以使用

FileCopy sDBsource, sDBdest

Instead of Scripting object而不是脚本对象

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

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