DoCmd.CopyDatabaseFile method (Access)

Copies the database connected to the current project to a Microsoft SQL Server database file for export.

Syntax

expression.CopyDatabaseFile (DatabaseFileName, OverwriteExistingFile, DisconnectAllUsers)

expression A variable that represents a DoCmd object.

Parameters

Name Required/Optional Data type Description
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 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 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 sign 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

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.