?
Solved

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

Posted on 2010-11-19
5
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Assisted Solution

by:Dale Fye
Dale Fye earned 300 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 1100 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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