Teedee Access VBA Teedee 2010
Moving a File
Home Tutorial Code Snippets

Move a File Using FSO

Use this function to move a file from one location to another. Typically this function will be called from a form where the Source and Destination are defined or user modifiable.


Microsoft Scripting Runtime reference

Function: MoveAttachment

Public Function MoveAttachment(strSource As String, strDest As String) As String     'Moves the file strSource to strDest     'Returns    ""      move successful     '           Error$  move failed         Dim fso As FileSystemObject     Dim fsoFile As File         Set fso = New FileSystemObject         'Does the source file exist     If fso.FileExists(strSource) Then         'Source exists. What about the destination         If fso.FileExists(strDest) Then             'Destination already exists - cannot move             MoveAttachment = "Destination already exists. Cannot overwrite"         Else             On Error Resume Next             fso.MoveFile strSource, strDest             If Err = 0 Then                 'Move successful                 MoveAttachment = ""             Else                 'Move failed                 MoveAttachment = Error$             End If         End If     Else         MsgBox "Cannot find: " & strSource, vbOKOnly + vbCritical, "Source not found"         MoveAttachment = "Cannot find source file"     End If End Function

File System Object (FSO)

The FileSystemObject, or FSO, is an often used component to access the web server's file system. For example, you can create files, read the contents of files, determine whether or not a folder or file exists, iterate through the contents of a folder or directory, or any other number of file system-related tasks.