expression.CopyDatabaseFile(DatabaseFileName, OverwriteExistingFile, DisconnectAllUsers)
expression Required. An expression that returns a DoCmd object.
DatabaseFileName Required Variant. The name of the file (and path) to which the current database is copied. If no path is specified, the current directory is used.
OverwriteExistingFile Optional Variant. Determines whether Microsoft Access overwrites the file specified by DatabaseFileName . True to overwrite the existing file. If the file doesn't already exist, this argument is ignored.
DisconnectAllUsers Optional Variant. Determines whether Access disconnects any users connected to the current database in order to make the copy. True to disconnect other users before copying the database file.
Remarks
The file name of the copy must have an .mdf extension in order to be recognized as a SQL Server database file.
The method fails and an error occurs if any of the following occurs:
-
DisconnectAllUsers is True but Access is unable to log off other users.
-
The method cancels a save operation by any open design sessions.
-
The destination file exists but OverwriteExistingFile was not set to True.
-
The destination file exists, but is in use by another application.
-
Access could not reconnect the original .mdf file.
-
The current user for the Access project doesn’t have system administrator privileges for the database server.
Example
This example copies the database connected to the current project to a SQL Server database file. If the file exists already, Access overwrites it, and any other users connected to the database are disconnected before the copy is made.
DoCmd.CopySQLDatabaseFile _
DatabaseFileName:="C:\Export\Sales.mdf", _
OverwriteExistingFile:=True, _
DisconnectAllUsers:=True