Microsoft KB Archive/172711: Difference between revisions

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - """ to """)
Line 62: Line 62:
</div>
</div>
<br />
<br />
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual.
This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.


</div>
</div>
Line 84: Line 84:


<ol>
<ol>
<li>Complete steps 1 through 4 from the &quot;Steps to Reproduce Behavior&quot; section later in this article.</li>
<li>Complete steps 1 through 4 from the "Steps to Reproduce Behavior" section later in this article.</li>
<li><p>Create a module and type the following lines in the Declarations section:<br />
<li><p>Create a module and type the following lines in the Declarations section:<br />
</p>
</p>
<pre class="fixed_text">      Option Explicit
<pre class="fixed_text">      Option Explicit
                         </pre>
                         </pre>
<pre class="codesample">      Declare Function apiCopyFile Lib &quot;kernel32&quot; Alias &quot;CopyFileA&quot; _
<pre class="codesample">      Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
       (ByVal lpExistingFileName As String, _
       (ByVal lpExistingFileName As String, _
       ByVal lpNewFileName As String, _
       ByVal lpNewFileName As String, _
Line 103: Line 103:
       '---------------------------------------------------------------
       '---------------------------------------------------------------
         Dim Result As Long
         Dim Result As Long
         If Dir(SourceFile) = &quot;&quot; Then
         If Dir(SourceFile) = "" Then
             MsgBox Chr(34) &amp; SourceFile &amp; Chr(34) &amp; _
             MsgBox Chr(34) &amp; SourceFile &amp; Chr(34) &amp; _
               &quot; is not valid file name.&quot;
               " is not valid file name."
         Else
         Else
             Result = apiCopyFile(SourceFile, DestFile, False)
             Result = apiCopyFile(SourceFile, DestFile, False)
Line 113: Line 113:
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<br />
<br />
<span class="kbd userinput"> CopyFile &quot;<path to Northwind.mdb>&quot;, &quot;C:\Northwind.mdb&quot;</span><br />
<span class="kbd userinput"> CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb"</span><br />
<br />
<br />
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.</li></ol>
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.</li></ol>
Line 122: Line 122:


<ol>
<ol>
<li>Complete steps 1 through 4 from the &quot;Steps to Reproduce Behavior&quot; section later in this article.</li>
<li>Complete steps 1 through 4 from the "Steps to Reproduce Behavior" section later in this article.</li>
<li><p>Create a module and type the following line in the Declarations section if it is not already there:<br />
<li><p>Create a module and type the following line in the Declarations section if it is not already there:<br />
</p>
</p>
Line 136: Line 136:
       '---------------------------------------------------------------
       '---------------------------------------------------------------
         Dim CopyString As String
         Dim CopyString As String
         If Dir(SourceFile) = &quot;&quot; Then
         If Dir(SourceFile) = "" Then
             MsgBox Chr(34) &amp; SourceFile &amp; Chr(34) &amp; _
             MsgBox Chr(34) &amp; SourceFile &amp; Chr(34) &amp; _
               &quot; is not a valid file name.&quot;
               " is not a valid file name."
         Else
         Else
             SourceFile = Chr(34) &amp; SourceFile &amp; Chr(34)
             SourceFile = Chr(34) &amp; SourceFile &amp; Chr(34)
             DestFile = Chr(34) &amp; DestFile &amp; Chr(34)
             DestFile = Chr(34) &amp; DestFile &amp; Chr(34)
             CopyString = &quot;COMMAND.COM /C COPY &quot; &amp; SourceFile &amp; _
             CopyString = "COMMAND.COM /C COPY " &amp; SourceFile &amp; _
               &quot; &quot; &amp; DestFile
               " " &amp; DestFile
             Call Shell(CopyString, 0)
             Call Shell(CopyString, 0)
         End If
         End If
Line 151: Line 151:
   change the line
   change the line


       CopyString = &quot;COMMAND.COM /C COPY &quot; &amp; SourceFile &amp; _
       CopyString = "COMMAND.COM /C COPY " &amp; SourceFile &amp; _


   to:
   to:


       CopyString = &quot;CMD.EXE /C COPY &quot; &amp; SourceFile &amp; _
       CopyString = "CMD.EXE /C COPY " &amp; SourceFile &amp; _
                         </pre></li>
                         </pre></li>
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<br />
<br />
<span class="kbd userinput"> CopyFile &quot;<path to Northwind.mdb>&quot;, &quot;C:\Northwind.mdb&quot;</span><br />
<span class="kbd userinput"> CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb"</span><br />
<br />
<br />
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.</li></ol>
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.</li></ol>
Line 197: Line 197:
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<li>To test this procedure, type the following line in the Debug window, and then press ENTER:<br />
<br />
<br />
<span class="kbd userinput"> CopyFile &quot;<path to Northwind.mdb>&quot;, &quot;C:\Northwind.mdb&quot;</span><br />
<span class="kbd userinput"> CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb"</span><br />
<br />
<br />
Note that you receive the error listed in the &quot;Symptoms&quot; section.</li></ol>
Note that you receive the error listed in the "Symptoms" section.</li></ol>




Line 207: Line 207:
== REFERENCES ==
== REFERENCES ==


For more information about the FileCopy statement, search the Help Index for &quot;FileCopy statement.&quot;
For more information about the FileCopy statement, search the Help Index for "FileCopy statement."


</div>
</div>

Revision as of 11:06, 21 July 2020

Article ID: 172711

Article Last Modified on 1/20/2007



APPLIES TO

  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition



This article was previously published under Q172711

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


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


This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

CAUSE

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

RESOLUTION

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:

          Option Explicit
                            
          Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
          (ByVal lpExistingFileName As String, _
          ByVal lpNewFileName As String, _
          ByVal bFailIfExists As Long) As Long
                            
  3. 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 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
                            
  4. To test this procedure, type the following line in the Debug 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:

          Option Explicit
                            
  3. If you are using Microsoft Windows 95, 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:
    
          CopyString = "CMD.EXE /C COPY " & SourceFile & _
                            
  4. To test this procedure, type the following line in the Debug 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.


STATUS

This behavior is by design.

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:

          Option Explicit
                            
  6. Type the following procedure:

          Sub CopyFile(SourceFile As String, DestFile As String)
             FileCopy SourceFile, DestFile
          End Sub
                            
  7. To test this procedure, type the following line in the Debug window, and then press ENTER:

    CopyFile "<path to Northwind.mdb>", "C:\Northwind.mdb"

    Note that you receive the error listed in the "Symptoms" section.


REFERENCES

For more information about the FileCopy statement, search the Help Index for "FileCopy statement."


Additional query words: file copy

Keywords: kbprb kbprogramming KB172711