Solved

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

Posted on 2010-11-19
5
463 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
ID: 34174951
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
ID: 34175975
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
ID: 34176235
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
ID: 34176250
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
ID: 34176408
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

837 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