VB6 Copyfile function gives "Access is Denied" error

Posted on 2009-04-29
Last Modified: 2012-06-21
Hi Experts,

One of my users has struck a problem with my VB6 application.
This code works fine on dozens of sites.

On his PC:
     App.Path = "C:\Program Files\circly5"
     strDataPath = "C:\Program Files\circly5\data"

The error message is:

Copy failed. Access is denied.
"C:\Program Files\circly5\circ32.pif","C:\Program Files\circly5\data\circ32.pif"


RetVal = CopyFile(App.Path & "\circ32.pif", strDataPath & "\circ32.pif", False) 'Overwrite if file exists (20061116)

        If RetVal = 0 Then  ' failure


            '20051117: added code to output error message corresponding to Err.LastDllError

            Dim strErrorMessage As String

            strErrorMessage = ""

            If Err.LastDllError <> 0 Then

                strErrorMessage = strGetErrorMessage(Err.LastDllError)

            End If


            strMessage = "Copy failed. " & strErrorMessage & vbCrLf & chrCommandLineDelimiter & App.Path & "\circ32.pif" & chrCommandLineDelimiter & "," & chrCommandLineDelimiter & strDataPath & "\circ32.pif" & chrCommandLineDelimiter

            MsgBox strMessage, vbInformation

            Logger.Log 1, strMessage

         Else  ' success

            'PrintToDebugFile "Copy succeeded."

        End If

Open in new window

Question by:LeighWardle
    1 Comment
    LVL 29

    Accepted Solution

    That error usually has something to do with user privileges but it can also be caused by another application that might have the original file open. In this case you may have a handle open to the file and copyfile will fail if you don't have any shared permissions.
    I am not sure what the number '20051117' means but the actual error number for Access Denied should be 5 if I can recall.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now