User intervention, abort .copyfile() with a button (1 Viewer)

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:05
Joined
Feb 28, 2001
Messages
26,999
The only way I know to abort a large file-copy via keyboard involves using a program intervention that would also break in on the app itself - which would certainly make it impossible for Access to manage the aftermath since at that point, it just got broken.

======
EDIT: But Colin knew of a way, so look at his stuff first. Still, there are many ways to approach this depending on how badly the file needs to be transferred, so you might wish to consider alternatives that let the user go on their merry way and not worry about the bloody file.
======

The stop-a-transfer problem comes about because Adam is technically correct even if his method of explaining is sometimes a bit off. Access is not capable of going multi-threaded. It is hard as all heck to make a parallel operation occur inside an Access app.

If you start a command to copy a file, whether via a File System Object or some other related method that stays within VBA, then you can try to hit the BREAK key. But it won't break until the copy operation is complete because that key doesn't interrupt the innards of Access. It can only break between lines of VBA code.

Your problem, as far as I can tell, is that your users get an itch to do something else and do extreme things to stop this copy operation. I am going to presume that you aren't allowed to shoot a few as examples (probably too much annoying paperwork), so let's try something else.

IF you have the ability to do a FileCopy or any other FSO operation to create files in a given destination, you might be able to create and then launch/spawn a separate batch job or an FTP Client batch job to upload to the server in parallel. In which case you would create the copy operation as a separate job. You would launch it and let it run merrily in background. Then your users wouldn't see that long delay. The only trick then would be if they are so stupid as to shut down their computer before they finish their job, then they shouldn't complain when being counseled about doing wrong.

Then there is the other issue - there is no reason they couldn't just open something else when the Access job is running. The only problem you would have at that point is if they start this putative long-running copy command and want to break away from that to use another part of the same Access app.

You COULD do something like write a separate Access app that can be launched in a way that it never becomes visible. Using a command-line argument, pass in the name of the file to be copied to this other machine and let it run. For examples, see this link:

https://support.office.com/en-us/article/command-function-fec67826-7fd7-48ed-a7aa-479c020ffaa4

which describes the Command function that returns at least part of the command line. You would launch this putative app using the Shell command.

https://www.myonlinetraininghub.com/vba-shell

Then when it is finished, you could have it pop up a message box that says "Your copy is complete." Then when they click OK, the box goes away and the secondary app does an Application.Quit for you so it goes away too.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 17:05
Joined
Jan 14, 2017
Messages
18,186
You've nailed it!

I missed out one important line of code.
To run it you just need this code
Code:
 Call apiFileCopy(FromPath, ToPath, True)

Or more simply
Code:
 apiFileCopy FromPath, ToPath, True

Where FromPath, ToPath are strings for the source folder and destination folder
The code will copy all files in the source folder

If you want the user to confirm whether existing files should be overwritten, then change True to False
 

daryll

Registered User.
Local time
Today, 10:05
Joined
Jan 2, 2018
Messages
49
I can provide more info if it helps

Your code returns only boolean, but i wan't to capture according to user's selection. What will be the return value if user select each of the following.

1. Copy and Replace
2. Don't Copy
3. Copy, but keep both files

Thanks.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 17:05
Joined
Jan 14, 2017
Messages
18,186
Your code returns only boolean, but i wan't to capture according to user's selection. What will be the return value if user select each of the following.

1. Copy and Replace
2. Don't Copy
3. Copy, but keep both files

Thanks.

The api functions returns true if successful and false if not
So for your 3 scenarios it would return true, false, true in that order

But perhaps you were referring to the boolean argument in the api itself which determines whether user intervention is required. As I wrote before:
If you want the user to confirm whether existing files should be overwritten, then change True to False

So to achieve those outcomes, use the api argument
1. True
2. False - followed by the user saying no to the Windows file copy prompt that follows if the file already exists



3. True - but this requires additional code to copy the file(s) or folder to another folder before overwriting. I use a folder called PreviousVersion for this purpose so I can restore files if anything goes wrong

Hope that makes sense
 

isladogs

MVP / VIP
Local time
Today, 17:05
Joined
Jan 14, 2017
Messages
18,186
Being as the only way to cancel the copy is by saying no to the API prompt, that's in line with what I wrote in the first two sentences.
 

daryll

Registered User.
Local time
Today, 10:05
Joined
Jan 2, 2018
Messages
49
The api functions returns true if successful and false if not

Instead of true or false, is it possible to get the actual returned code for that three scenarios?
 

Attachments

  • API prompt.JPG
    API prompt.JPG
    53.3 KB · Views: 39
Last edited:

isladogs

MVP / VIP
Local time
Today, 17:05
Joined
Jan 14, 2017
Messages
18,186
Not that I know of.
As already stated, the api is a Boolean function - true or false.
That dialog obviously is based on additional code similar in concept to that already explained.
 
Last edited:

Users who are viewing this thread

Top Bottom