Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

APICopyFIle does nothing

Posted on 2006-10-19
6
Medium Priority
?
648 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 65

Accepted Solution

by:
rockiroads earned 750 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

596 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