Avatar of Buck Beasom
Buck Beasom
Flag for United States of America asked on

Capturing FileCopy fails in Access Code

I am using the File Copy to deploy upgrades to an application, and I am trying to catch instances where the copy fails because the User has the application open. I am trying to use this code to capture the failure:

FileCopy strFileCopyFrom, strFileCopyTo
If Err <> 0 Then

The Err is not capturing the error. I am resetting it to 0 before each copy attempt.

Is there a better method for catching the instances where the Copy fails?
Microsoft Access

Avatar of undefined
Last Comment
Buck Beasom

8/22/2022 - Mon
Jim Dettman (EE MVE)

What is your On Error statement?

 Should be On Error Resume Next

Jim Dettman (EE MVE)


On Error Resume Next
Err = 0

FileCopy strFileCopyFrom, strFileCopyTo

If Err <> 0 Then
John Tsioumpris

When do you perform this copy...
The simple way is to copy the file when the user logins...
If in the mid day you need that you probably need to check Windows Process  to see if Ms Access is active ..like here : http://www.wisesoft.co.uk/scripts/vbscript_check_if_a_process_is_running.aspx
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Anders Ebro (Microsoft MVP)

I believe its because you should be using:
If Err.Number <> 0 Then

Open in new window

Buck Beasom

Thanks to all.

In no particular order:

I have successfully used just the "If Err <> 0" in other cases, notably when a User generated process tried to save over an Excel file that is already open.

I AM using the On Err Resume Next. (Actually, Jim, I think I may have gotten the code from you some time back!) And I am setting Err to Zero before each attempt so I don't inherit a previous fail.

The problem seems to be that if the copy fails, it is not changing Err from 0 to another value. I actually set up the first copy effort to a file I KNEW the user had open so I could throw the error and capture it. But when I walk through the code line by line and the File Copy runs but fails, the Err is remaining = 0 so I can't capture it.

Since "File Copy" is really just executing a DOS command (remember those days!) I'm afraid that - unlike an error that would occur inside the Access environment (like a run time error) - the failure of the File Copy command doesn't throw a value into the Err variable.

I can't use the approach of copying the new app down on login (which VB does - or did when I was using it) automatically because the app being copied is the one where the Login occurs. So - unless there is something I don't know about - the copy would ALWAYS fail because the file would be open when the File Copy command was called. So at the moment I am stuck with having to examine EACH destination folder to see if the new version copied over. I'm wondering if maybe there is some way to display each destination folder's contents - with the Date Stamp - so I could see any that were not the current version. A little klugy - but better than nothing.
John Tsioumpris

you can use a simple bat file for the copy on login
just xcopy source destination
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

You should study my article on exactly this subject:

Deploy and update a Microsoft Access application with one click

It demonstrates how to use a shortcut and a script to avoid all the troubles like the one you are facing.
Buck Beasom

Gustav. I am going to dig into what you provided. I'm pretty sure it will come in useful in a number of settings. Will get back to you as soon as I have completed my study. Thank you!
Anders Ebro (Microsoft MVP)

You could also look into using the FileSystemObject for your copy needs. It should have better error feedback:
FileSystemObject CopyFile
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Buck Beasom

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.