Solved

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

Posted on 2010-11-19
5
452 Views
1 Endorsement
Last Modified: 2012-06-27
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.  
1
Comment
Question by:UTSWPeds
  • 2
  • 2
5 Comments
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 75 total points
Comment Utility
Use the Dir function.  It will return the name of the file if it exists:

?Dir("C:\Folder1\Retirement_Copy.xlsx")

will return

Retirement_Copy.xlsx if the file exists, or an empty string "" if not.
0
 

Author Comment

by:UTSWPeds
Comment Utility
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.
0
 
LVL 13

Expert Comment

by:Torrwin
Comment Utility
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

0
 
LVL 13

Accepted Solution

by:
Torrwin earned 275 total points
Comment Utility
So, you would use that like so:
Dim sPath as String

sPath = "C:\Folder1\Retirement.xlsx"

If FileExists(sPath) Then
   msgbox "Your Success Message"
Else
   msgbox "Your Fail Message"
End if

Open in new window

0
 

Author Closing Comment

by:UTSWPeds
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now