MS Access – VBA – Copy A Folder

Below is a simple little function which will allow you to make a copy of a folder since it uses the File Scripting Object it can be used in all VBA Applications (Word, Excel, Access, PowerPoint, …).

'---------------------------------------------------------------------------------------
' Procedure : CopyFolder
' Author    : CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Copy a folder
' 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:
' ~~~~~~~~~~~~~~~~
' sFolderSource         Folder to be copied
' sFolderDestination    Folder to copy to
' bOverWriteFiles       Whether to overwrite file(s) if the folder already exists
'
' Usage Example:
' ~~~~~~~~~~~~~~~~
' CopyFolder("C:\Temp", "D:\Development\New", True)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2010-Nov-14                 Initial Release
'---------------------------------------------------------------------------------------
Function CopyFolder(sFolderSource As String, sFolderDestination As String, _
                    bOverWriteFiles As Boolean) As Boolean
On Error GoTo Error_Handler
    Dim fs As Object
    
    CopyFolder = False
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFolder sFolderSource, sFolderDestination, bOverWriteFiles
    CopyFolder = True

Error_Handler_Exit:
    On Error Resume Next
    Set fs = Nothing
    Exit Function

Error_Handler:
    If Err.Number = 76 Then
        MsgBox "The 'Source Folder' could not be found to make a copy of.", _
                vbCritical, "Unable to Find the Specified Folder"
    Else
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: CopyFolder" & vbCrLf & _
               "Error Description: " & Err.Description, _
               vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function

6 responses on “MS Access – VBA – Copy A Folder

  1. Krofinzki

    When trying to use this I get this error message:

    “Compile Error:
    Expected: =”

    I put it in the VBA code for a form, and I made an OnClick event on a button. Then I called the function with this:

    CopyFolder(“X:\Testmapp0000 Mall”, “X:\Testmapp\Test”, True)

    When just pressing Enter after finishing the line is when I get the error.

    Thankful for any advice on how to fix it!

    Cheers,
    Krof

    1. admin Post author

      When creation an expression you have to preceed it with an =. So you should try:

      =CopyFolder(“X:\Testmapp0000 Mall”, “X:\Testmapp\Test”, True)

  2. Krofinzki

    Sorry, my lack of experience is shining through…

    I discovered on another button OnClick event I had I had to set up a Dim and then specify the command I wanted to happen as the = for that Dim. It wasn’t needed on other AfterUpdate events, but it was on this OnClick event…

    There’s lots of little logical rules of Access VBA scripting that I don’t grasp yet and this one puzzled me greatly.

    Cheers for the help anyway and thanks a lot for the code! 🙂

    /Krof