You may store file attachments in Microsoft Access by utilizing the Attachment field in table design. Unlike other field types, the Value property of the field is actually a recordset of the attached files. These VBA functions allow you to delete the attachments by utilizing the recordset.
DeleteAttachmentsFromField() – Deletes all of the attachments in the Attachment field for all of the records in a recordset passed to the function.
Here is an example:
Private Sub cmdDeleteAllAttachments_Click()
' This procedure runs when the control is clicked.
On Error GoTo Err_Handler
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT Speakers.Legal " _
& "FROM Speakers"
Set rst = CurrentDb.OpenRecordset(strSQL)
DeleteAttachmentsFromField rst, "Legal"
Me.Refresh
Exit_Proc:
On Error Resume Next
rst.Close
Exit Sub
Err_Handler:
MsgBox Err.Number & " " & Err.Description, vbCritical, "cmdDeleteAllAttachments_Click()"
Resume Exit_Proc
End Sub
DeleteAttachmentsFromControl() – Deletes all of the attachments in the Attachment field for a particular record being viewed in a form.
Here is an example:
Private Sub cmdDeleteAttachments_Click()
' This procedure runs when the control is clicked.
On Error GoTo Err_Handler
DeleteAttachmentsFromControl Me, Me.attLegal
Exit_Proc:
On Error Resume Next
Exit Sub
Err_Handler:
MsgBox Err.Number & " " & Err.Description, vbCritical, "cmdDeleteAttachments_Click()"
Resume Exit_Proc
End Sub
NOTE: Once you run the code, even if you are still in edit mode for the form record, the ESC key does not bring back the attachments.
basDeleteAttachments
' basDeleteAttachments() Version 1.0.0
' Copyright © 2013 Extra Mile Data, www.extramiledata.com.
' For questions or issues, please contact [email protected].
' Use (at your own risk) and modify freely as long as proper credit is given.
'
Public Function DeleteAttachmentsFromField(ParentRST As DAO.Recordset, _
AttFieldName As String) As Boolean
' This procedure deletes the attachments from the AttFieldName field
' for all records in the ParentRST recordset.
On Error GoTo Err_Handler
Dim rst As DAO.Recordset
' Start at the first record and loop through all the ParentRST
' records.
ParentRST.MoveFirst
Do Until ParentRST.EOF
' Get the attachment recordset.
Set rst = ParentRST.Fields(AttFieldName).Value
' Exit if there are no attachments.
If rst.BOF And rst.EOF Then
' Do nothing.
Else
' Start at the first attachment record and loop through
' all the attachment records, deleting each one.
With rst
.MoveFirst
Do Until .EOF
.Delete
.MoveNext
Loop
.Close
End With ' rst
End If
ParentRST.MoveNext
Loop ' ParentRST
DeleteAttachmentsFromField = True
Exit_Proc:
On Error Resume Next
rst.Close
Exit Function
Err_Handler:
MsgBox Err.Number & " " & Err.Description, vbCritical, _
"DeleteAttachmentsFromField()"
DeleteAttachmentsFromField = False
Resume Exit_Proc
End Function
'
Public Function DeleteAttachmentsFromControl(AttForm As Form, _
AttControl As Control) As Boolean
' This procedure deletes the attachments from the control source field
' of the AttControl control on the AttForm for the current AttForm record.
On Error GoTo Err_Handler
Dim rstControl As DAO.Recordset
Dim rstForm As DAO.Recordset
Dim strCS As String
' Get the name of the attachment field.
strCS = AttControl.ControlSource
' Get the form recordset.
Set rstForm = AttForm.RecordsetClone
' Sync the clone with the form so that we work with the
' correct record.
rstForm.Bookmark = AttForm.Bookmark
' Get the attachment recordset.
Set rstControl = rstForm.Fields(strCS).Value
' Exit if there are no attachments.
If rstControl.BOF And rstControl.EOF Then
DeleteAttachmentsFromControl = True
GoTo Exit_Proc
End If
' Start at the first attachment record and loop through
' all the records, deleting each one.
With rstControl
.MoveFirst
Do Until .EOF
.Delete
.MoveNext
Loop
End With ' rstcontrol
' Requery the control.
AttControl.Requery
DeleteAttachmentsFromControl = True
Exit_Proc:
On Error Resume Next
rstControl.Close
rstForm.Close
Exit Function
Err_Handler:
MsgBox Err.Number & " " & Err.Description, vbCritical, _
"DeleteAttachmentsFromControl()"
DeleteAttachmentsFromControl = False
Resume Exit_Proc
Resume