55

I have an access file that I regularly need to copy to another directory, replacing the last version. I would like to use an Excel macro to achieve this, and would also like to rename the file in the process.

   fileName = "X:\Database\oldName.accdb"
   copyDestination = "Y:\dbstore\"
   newName = "newName.accdb"

Is there an easy way of doing this?

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85
harryg
  • 23,311
  • 45
  • 125
  • 198

2 Answers2

81

Use the appropriate methods in Scripting.FileSystemObject. Then your code will be more portable to VBScript and VB.net. To get you started, you'll need to include:

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")

Then you could use

Call fso.CopyFile(source, destination[, overwrite] )

where source and destination are the full names (including paths) of the file.

See https://learn.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/copyfile-method

Darren Bartrup-Cook
  • 18,362
  • 1
  • 23
  • 45
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
81

This method is even easier if you're ok with fewer options:

FileCopy source, destination
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Jon
  • 1,189
  • 11
  • 17
  • 5
    Nice and simple, however when I use this solution and the Access database to be copied is opened, I get an "Access denied" error. This does not happen when using the FileSystemObject. – Jörg Brenninkmeyer Dec 01 '17 at 08:53