Solved

How to trap 'Access Denied' with CopyFile

Posted on 2003-11-03
4
944 Views
Last Modified: 2008-03-17
Hello,
I am using the CopyFile API function to copy a file from the user's machine to a network directory.  Under most circumstances it should work fine, but I have identified two conditions where it might fail and I need to be able to trap them:
1. the destination file already exists (in which case I need to prompt them for the next step - it may be overwrite, it may be rename, it may be cancel, up to them)
2. they don't have write access to this network directory (in which case I need to throw an error and bail)

All I have been able to do so far is test if CopyFile(parameters) = 0, but this doesn't tell me very much.

By the way here is the declaration in case it helps.
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long

Thanks for your help
0
Comment
Question by:pteehan
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9671405
Call GetLastError API immidiately after CopyFile:

Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long

If it returns 5, this means Access is Denied.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 9671479
Read also this article which gives the generic solution:

http://www.freevbcode.com/ShowCode.Asp?ID=507
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9671663
Put this abel in that form.
on error goto ErrHanlder:

ErrHandler:
If err.number = 70 or err.number = 75 then
 'Ignores Access Denied
end if
0
 
LVL 2

Author Comment

by:pteehan
ID: 9671957
Thanks AlexFM.
GetLastError didn't seem to work but from the article you linked I noticed Err.LastDllError - this is what I needed.  Exactly what I was looking for!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 91
Question about INSTR  - need to extract data MS ACCESS query 6 54
TT Auto Dashboard 13 85
Added a column screws up code 5 50
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

864 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now