Search code examples
vbams-accesspermissionsms-access-2013filesystemobject

Move folder across network using vba


I am trying to figure out how to properly move folders on a Network Share using VBA code from an MS Access Form.

Currently I am trying to use the FileSystemObject.MoveFolder method but keep running into a "Permissions Denied" error.

I have referenced this SO question and none of the top suggestions worked. Permission denied on CopyFile in VBS

I have verified that the SourcePath and the DestinationPath both are valid by using this function to MoveFolders on my local machine. I have also verified that both Folders have the appropriate network permissions. See Below

Source Folder Destination Folder

So my question is, is there a way to provide credentials with the FileSystemObject? or should I be using a different function entirely?

EDIT:

I have verified that I can move the folders manually. I have tried the function with and without files in the source folder.

I also have tried hardcoding the source and destination paths into the FSO.MoveFolder Command

Private Sub Check6_AfterUpdate()

    On Error GoTo Err_DormantHandler
    Dim response As String
    Dim client As String
    Dim FSO As Object
    Dim fromPath As String
    Dim toPath As String
    Set FSO = CreateObject("Scripting.Filesystemobject")

    client = Me.CustomerName.Value
    fromPath = "P:\__Active_Clients\" & client
    toPath = "R:\Dormant_Clients\"

    If Me.Check6.Value = True Then
        response = MsgBox("Would you like to automatically move the " & client & " folder to the dormant folder?", vbYesNo)

        If response = vbYes Then
            If FSO.FolderExists(fromPath) = False Then
                MsgBox fromPath & " doesn't exist."
                Exit Sub
            End If
            If FSO.FolderExists(toPath) = False Then
                MsgBox toPath & " doesn't exist."
                Exit Sub
            End If

            FSO.MoveFolder source:=fromPath, destination:=toPath
            MsgBox "The customer folder has been moved to " & vbNewLine & toPath, vbOKOnly
        End If

        If response = vbNo Then
            MsgBox "The customer folder will NOT be moved to dormant"
            Exit Sub
        End If
    End If


Exit_DormantHandler:
    Exit Sub

Err_DormantHandler:
    MsgBox "Error# " & Err & vbNewLine & "Description: " & Error$
    Resume Exit_DormantHandler

End Sub

Solution

  • I'd try with xcopy from windows :

    Sub Test()
      XCopy "C:\source", "C:\destination\", elevated:=False
    End Sub
    
    Public Sub XCopy(source As String, destination As String, Optional elevated = False)
      Static shell As Object
      If shell Is Nothing Then Set shell = CreateObject("Shell.Application")
    
      Dim vArguments, vOperation
      vArguments = "/E /Y """ & source & """ """ & destination & """"
      vOperation = IIf(elevated, "runas", "")
    
      shell.ShellExecute "xcopy.exe", vArguments, "", vOperation, 0
    End Sub