Applecore Pages on Microsoft Access

Deleting a File to the Recycle Bin

One of the more annoying things about the Kill statement in Access is that it deletes files permanently, bypassing the Recycle Bin. Often you may instead want to allow user-recovery of these files. Here is a way of doing this, using an API call. Copy and paste the code below into a new module, and you can then send files to the Recycle Bin:

Private Declare Function apiSHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As SHFILEOPSTRUCT) As Long
 
Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As String
End Type
 
Private Const FOF_ALLOWUNDO = &H40
Private Const FO_DELETE = &H3
Private Const FOF_NOCONFIRMATION = &H10
 
Sub sDelete(strFile As String)
'   Procedure to delete a file to the Recycle Bin instead of deleting it outright
'   Accepts:
'       strFile - a full path/file name to a file that is to be deleted.

    Dim typSHFile As SHFILEOPSTRUCT
    Dim lngReturn As Long
    If Len(Dir(strFile)) = 0 Then
        MsgBox "File does not exist.", vbOKOnly, " "
    Else
        With typSHFile
            .pFrom = strFile & vbNullChar & vbNullChar
            .wFunc = FO_DELETE
            .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
        End With
        lngReturn = apiSHFileOperation(typSHFile)
    End If
End Sub

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 13:53:14