• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3145
  • Last Modified:

Prevent filecopy from overwriting existing file in access vba

Here is the module I'm using;

Option Compare Database

' Procedure : CopyFile
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Copy a file
'             Overwrites existing copy without prompting
'             Cannot copy locked files (currently in use)
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
' Input Variables:
' ~~~~~~~~~~~~~~~~
' strSource - Path/Name of the file to be copied
' strDest - Path/Name for copying the file to
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' 1         2007-Apr-01             Initial Release
Function CopyFile(strSource As String, strDest As String) As Boolean
On Error GoTo CopyFile_Error
    FileCopy strSource, strDest
    CopyFile = False
    Exit Function
    If Err.Number = 0 Then
    ElseIf Err.Number = 70 Then
        MsgBox "The file is currently in use and therfore is locked and cannot be copied at this" & _
               " time.  Please ensure that no one is using the file and try again.", vbOKOnly, _
               "File Currently in Use"
    ElseIf Err.Number = 53 Then
        MsgBox "The Source File '" & strSource & "' could not be found.  Please validate the" & _
               " location and name of the specifed Source File and try again", vbOKOnly, _
               "File Currently in Use"
        MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & "Error Number: " & _
               Err.Number & vbCrLf & "Error Source: ModExtFiles / CopyFile" & vbCrLf & _
               "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
    End If
    Exit Function
End Function

Open in new window

Then I call the file to copy during a command on click event as so:

Call FileCopy("C:\Users\jmeyer\Desktop\New folder\Blank.jpg", "\\sfile0\e\data\jmeyer\Jarred\iPadDocs\" & Me.KeyField & ".jpg")

The problem is, if the file already exists it will just replace it with the blank where I would like it to just do nothing. In other words if the file exists then do nothing else copy the blank file and save it as so.

Thanks for the advice!
1 Solution
You can check for an existing file like this:

If Dir("YourFilePath") & "" = ""  Then
     Call FileCopy...
   ' Do nothing
End if
SeyerITAuthor Commented:
Perfect! Thanks!

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now