Solved

APICopyFIle does nothing

Posted on 2006-10-19
6
625 Views
Last Modified: 2007-12-19
From Access 2002 (XP) VB code.

I have:

"
Option Compare Database
Option Explicit

Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
        (ByVal lpExistingFileName As String, _
         ByVal lpNewFileName As String, _
         ByVal bFailIfExists As Long) As Long
         
Sub CopyFile(SourceFile As String, DestFile As String)
'---------------------------------------------------------------
' PURPOSE: Copy a file on disk from one location to another.
' ACCEPTS: The name of the source file and destination file.
' RETURNS: Nothing
'---------------------------------------------------------------
    Dim result As Long
    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        result = apiCopyFile(SourceFile, DestFile, False)
    End If
     
End Sub
"

The APICopyFIle returns 0 but does not copy the file(s) across! And, before you ask, the original file exists in the path I give it. So does the target floder.

Example of use:

"
        CopyFile COU30 & "Cg3 US " & username & ".mde", COU3
"

where COU30 is string "C:\Program Files\Fred 3\"
and COU3 is string "C:\Program Files\Fred 3.1\"
and username is "example3"

Any ideas, however insultingly obvious they should have been to me...

Many thanks.
0
Comment
Question by:Peborgh
[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
  • 3
  • 2
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 17767049
you can just use the Access command FileCopy


    FileCopy SourcePath, DestPath
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17767057
Perhaps destfile is not writable, permissions not there?



As a alternative, have u tried FileCopy? builtin func, no api required


Sub CopyFile2(SourceFile As String, DestFile As String)

    Dim result As Long

    On Error Resume Next

    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        Err.Clear
        If DIr$(DestFile) <> "" Then kill DestFile
        If Err.Number <> 0 Then
             msgbox "Crap, I cant delete the damn destination file." & vbcrlf & err.description
        Else
            FileCopy SourceFile, DestFile
            If Err.Number <> 0 Then
                msgbox "Not having a good day as the file copy has failed.." & vbcrlf & err.description
            End if
        End if
    End If
     
End Sub

0
 

Author Comment

by:Peborgh
ID: 17768151
I was using my CopyFile because FileCopy died when the destination file already existed and, I think, because the destination had to be afile and could not be a folder. I am not sure any more, sorry. Or maybe it was that I could not copy a wildcarded bucnh of files at a time...

If you can aasure me that FileCopy will do these things I require, I will repent and return to the fold!

Many thanks,

peter
0
Technology Partners: 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!

 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 17768622
Ok, so u want the option to copy to folders

Lets see if this works


Sub CopyFile2(SourceFile As String, DestFile As String)

    Dim result As Long

    On Error Resume Next

    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        Err.Clear
        'Check if DestFile is a folder

        If Dir$(DestFile,vbDirectory) <> "" Then
            If Right$(DestFile,1) <> "\" Then DestFile = DestFile & "\"
            DestFile = DestFile & Dir$(SourceFile)
        End if

        If DIr$(DestFile) <> "" Then kill DestFile
        If Err.Number <> 0 Then
             msgbox "Crap, I cant delete the damn destination file." & vbcrlf & err.description
        Else
            FileCopy SourceFile, DestFile
            If Err.Number <> 0 Then
                msgbox "Not having a good day as the file copy has failed.." & vbcrlf & err.description
            End if
        End if
    End If
     
End Sub



dont do wildcards though
0
 

Author Comment

by:Peborgh
ID: 17773664
All, thank you for all your trouble, especially Bumpy Way.

In the end, the solution I wnet for that does all I want, is to use FileSystemObject.CopyFile. Points to Crooked Mile for trying so hard...
peter
0
 

Author Comment

by:Peborgh
ID: 17773668
... and humourous error messages.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

688 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