Solved

APICopyFIle does nothing

Posted on 2006-10-19
6
604 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 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

770 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