ACC2000: FileCopy Statement May Not Copy Open FilesID: Q207703
|
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
The file is currently open, which prevents the FileCopy statement from copying the file.
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.aspInstead of using the FileCopy statement, use one of the following methods to programmatically copy the file.
Option Explicit
Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
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
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.
Option Explicit
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:
CopyString = "CMD.EXE /C COPY " & SourceFile &
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.
Option Explicit
Sub CopyFile(SourceFile As String, DestFile As String)
FileCopy SourceFile, DestFile
End Function
CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb"
Note that you receive the error message mentioned in the "Symptoms" section.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