Problem with CopyFile

agehoops

Registered User.
Local time
Today, 09:18
Joined
Feb 11, 2006
Messages
351
Got a slight problem with a file copy function in my System.

I have the following code in a separate module:

Code:
Option Compare Database

     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

Then, in another form, I've got:
Code:
CopyFile BackEndLocation, strFolderName & "BackEnd.mdb"

To call it with the variables stated.

If I put the brackets around the variable it says it's expecting an =, if I leave it as it is, it doesn't produce any errors, and seems to run, however it doesn't actually copy the file.

Also, if I put the code in the form itself, then other things go wrong which work perfectly without it.

This code USED to work fine and suddenly stopped?
Can anyone see what is wrong with it?

Thanks
 
Just Curious, why not use the FileSystemObject instead?
 
Not really sure tbh. When i was first looking for solutions, this was the easiest (that I found) and got working. Now it doesn't work and I don't know why? :(

Do you have a link or the code for the FileSystemObject??
 
Does that work for UNC names though? Basically what i'm using it for is making a copy of the backend that is currently linked to a location that the user specifies, but on some of the machines, the backend may be located at something like:

//PC1/documents...

Will this still work??
 
yea it works the same... as long as the other computer exists :)
 
haha well yea :P

with that link what does it mean when it says :

Note you need to include a reference to the Windows Script Host


How???
 
Ok i'm using this:

Code:
        Dim FSO
        Dim sSourceFile
        Dim sDestinationFile

        Set FSO = CreateObject("Scripting.FileSystemObject")

        sSourceFile = BackEndLocation
        sDestinationFile = strFolderName & "BackEnd.mdb"

        FSO.CopyFile sSourceFile, sDestinationFile

        ' Clean Up
        Set oFSO = Nothing

And it's doing nothing. Same problem I had before, no errors or anything, just not copying the file
 

Users who are viewing this thread

Back
Top Bottom