FredixBlog


Just my log about anything I could find enjoyable.



Copy a file

author Posted by: fredometro on date Jan 1st, 2004 | filed Filed under: MS-Access

This function copies a file
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_FILESONLY = &H80

Private Type SHFILEOPSTRUCT
      hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Private Declare Function SHFileOperation Lib “shell32.dll” _
    Alias “SHFileOperationA” (lpFileOp As SHFILEOPSTRUCT) As Long

Public Function MyFileCopy(txtSource, txtDestination)
Dim lFileOp As Long
Dim lresult As Long
Dim lFlags As Long
Dim SHFileOp As SHFILEOPSTRUCT
Dim chkSilent, chkYesToAll, chkRename, chkDir, chkFilesOnly As Boolean

DoCmd.Hourglass True

lFileOp = FO_COPY

chkYesToAll = True
chkSilent = True

If chkSilent Then lFlags = lFlags Or FOF_SILENT
If chkYesToAll Then lFlags = lFlags Or FOF_NOCONFIRMATION
If chkRename Then lFlags = lFlags Or FOF_RENAMEONCOLLISION
If chkDir Then lFlags = lFlags Or FOF_NOCONFIRMMKDIR
If chkFilesOnly Then lFlags = lFlags Or FOF_FILESONLY

‘ NOTE: By adding the FOF_ALLOWUNDO flag you can move
‘ a file to the Recycle Bin instead of deleting it.

With SHFileOp
    .wFunc = lFileOp
    .pFrom = txtSource & vbNullChar & vbNullChar
    .pTo = txtDestination & vbNullChar & vbNullChar
    .fFlags = lFlags
End With
lresult = SHFileOperation(SHFileOp)

‘ If User hit Cancel button while operation is in progress,
‘ the fAborted parameter will be true

DoCmd.Hourglass False
‘If lresult <> 0 Or SHFileOp.fAborted Then Exit Function
‘MsgBox “Operation Complete”, vbInformation, “File Operations”
MyFileCopy = lresult
End Function

tag17 Responses to “Copy a file”

  1. ovaliawew Said,

    This is my first post I’d like to thank you for such a terrific quality site!
    Was thinking this would be a perfect way to introduce myself!

    Sincerely,
    Monte Phil
    if you’re ever bored check out my site!
    [url=http://www.partyopedia.com/articles/farm-party-supplies.html]farm Party Supplies[/url].

     Add A Comment

You must be logged in to post a comment.