Solved

APICopyFIle does nothing

Posted on 2006-10-19
6
596 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
  • 3
  • 2
6 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Security Threats Are You Missing?

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

12 Experts available now in Live!

Get 1:1 Help Now