ACC2000: FileCopy Statement May Not Copy Open Files

ID: Q207703


The information in this article applies to:

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SYMPTOMS

When you programmatically copy a file with the FileCopy statement in Visual Basic for Applications, you may receive the following error message:

Run-time error '70'
Permission denied


CAUSE

The file is currently open, which prevents the FileCopy statement from copying the file.


RESOLUTION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
Instead of using the FileCopy statement, use one of the following methods to programmatically copy the file.

WARNING: The following functions enable you to copy an open file. If the source file is changed while the copy operation is in process, the destination file may be incomplete or may become corrupted.

Method 1 - Calling the CopyFile() function from the Windows API

One method to programmatically copy a file is to call the CopyFile() function from the Microsoft Windows API. To call the CopyFile() function from the Microsoft Windows API, follow these steps:
  1. Complete steps 1 through 4 from the "Steps to Reproduce Behavior" section later in this article.


  2. Create a module and type the following lines in the Declarations section:


  3. 
    Option Explicit
    
    Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
    (ByVal lpExistingFileName As String, _
    ByVal lpNewFileName As String, _
    ByVal bFailIfExists As Long) As Long 
  4. Type the following procedure:


  5. 
    Sub CopyFile(SourceFile As String, DestFile As String)
    '---------------------------------------------------------------
    ' PURPOSE: Copy a file on disk from one location to another.
    ' ACCEPTS: The name of the source file and destination file.
    ' RETURNS: Nothing
    '---------------------------------------------------------------
      Dim Result As Long
       If Dir(SourceFile) = "" Then
          MsgBox Chr(34) & SourceFile & Chr(34) & _
             " is not valid file name."
       Else
          Result = apiCopyFile(SourceFile, DestFile, False)
       End If
    End Sub 
  6. To test this procedure, type the following line in the Immediate window, and then press ENTER:
    
    CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb" 
    Note that Northwind.mdb is copied to the root folder of drive C, even though it is currently open in another instance of Microsoft Access.


Method 2 - Calling the MS-DOS Copy Command

Another method to programmatically copy a file is to call the MS-DOS Copy command from a Shell() function in Visual Basic for Applications. To call the MS-DOS Copy command, follow these steps:
  1. Complete steps 1 through 4 from the "Steps to Reproduce Behavior" section later in this article.


  2. Create a module and type the following line in the Declarations section if it is not already there:


  3. 
    Option Explicit 
  4. If you are using Microsoft Windows 95 or later, type the following procedure:
    
    Sub CopyFile(SourceFile As String, DestFile As String)
    '---------------------------------------------------------------
    ' PURPOSE: Copy a file on disk from one location to another.
    ' ACCEPTS: The name of the source file and destination file.
    ' RETURNS: Nothing
    '---------------------------------------------------------------
       Dim CopyString As String
       If Dir(SourceFile) = "" Then
          MsgBox Chr(34) & SourceFile & Chr(34) & _
             " is not a valid file name."
       Else
          SourceFile = Chr(34) & SourceFile & Chr(34)
          DestFile = Chr(34) & DestFile & Chr(34)
          CopyString = "COMMAND.COM /C COPY " & SourceFile & _
             " " & DestFile
          Call Shell(CopyString, 0)
       End If
    End Sub 
    If you are using Microsoft Windows NT, use the same procedure, but change the line:
    CopyString = "COMMAND.COM /C COPY " & SourceFile &
    to:


  5. CopyString = "CMD.EXE /C COPY " & SourceFile &
  6. To test this procedure, type the following line in the Immediate window, and then press ENTER:
    
    CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb" 
    Note that Northwind.mdb is copied to the root folder of drive C, even though it is currently open in another instance of Microsoft Access.



MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Microsoft Access.


  2. Open the sample database Northwind.mdb.


  3. Start a new instance of Microsoft Access.


  4. Create a new blank database.


  5. Create a module and type the following line in the Declarations section if it is not already there:


  6. 
    Option Explicit 
  7. Type the following procedure:


  8. 
    Sub CopyFile(SourceFile As String, DestFile As String)
       FileCopy SourceFile, DestFile
    End Function 
  9. To test this procedure, type the following line in the Immediate window, and then press ENTER:
    
    CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb" 
    Note that you receive the error message mentioned in the "Symptoms" section.



REFERENCES

For more information about the FileCopy statement, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words: file copy


Keywords          : kberrmsg kbprg kbdta MdlGnrl 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 6, 1999