Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
474 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Progress
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

610 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