Link to home
Start Free TrialLog in
Avatar of UTSWPeds
UTSWPedsFlag for United States of America

asked on

How can I use FileSystemObject in Access 2007 to confirm that a file has been copied sucessfully?

I have a command button in a MS Access 2007 Database that I use to copy a file.  I want to confirm that the file has copied successfully, and I think FileSystemObject is the best way to do it.   My command button executes the following line of code on after the On Click event:

FileCopy "C:\Folder1\Retirement.xlsx", "C:\Folder1\Retirement_Copy.xlsx"

After this line of code is run, I want to run a program that searches C:\Folder1 to validate that Retirement_Copy.xlsx exists.  It it does exist, I'd like to have a message box that tells the user that the file copied successfully.  

If it does NOT exist, I'd like a message box to inform the user that the file did not copy successfully.

Thanks.  I'm getting better with VBA build in functions, but still have a long way to go.  
SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UTSWPeds

ASKER

This gets me part way there.  

   If Dir("C:\Folder1\Retirement_Copy.xlsx") = "" Then
    MsgBox "File Did Not Copy - Please be sure that the source file is not already open or in use by another program"
   Else
    MsgBox "File Copied Successfully"
   End If

I tested it by having the "source" file open when I clicked the button and it didn't give me the first MsgBox, as I suspected, it gave me a "Permission Denied" warning.
Here's the function I use:
'Determine whether or not a file exists at the given path
Public Function FileExists(filePath) As Boolean
    On Error GoTo Error_Handler
    
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    FileExists = fs.FileExists(filePath)
    
Clean_Up:
    Set fs = Nothing
    
    Exit Function
Error_Handler:
    msgbox err.descriptoin
    FileExists = False
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works.  My preference would have been to have something purely done with Build-In funtions and contained within the On Click event of the command button (instead of building a function to call later in the process, as I'm not to the level of writing my own functions yet) but this solution did work very effectively.

Points were split, but it was greatly appreciated how Torrwin gave a little more explaination and direction than just the code, as that helped me know how to properly implement his instructions.  For my level of VBA knowledge, this was very important.