Total Visual SourceBook

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office 2007 and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

Separate version for Access/Office 2003, 2002, and 2000.

Separate version for:
Access 97/95


View all FMS products for Microsoft AccessAll Our Microsoft Access Products

SourceBook Info:

Why SourceBook?

 

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

 

 

 

Microsoft Access, Office/VBA, VB6 Modules royalty-free source code libraryMicrosoft Access Modules royalty-free source code libraryMicrosoft Access VBA and VB6 Module Library Free trial of Total Visual SourceBook for Microsoft Access, Office, VB6 and VBA

Module: AttachmentFields in Category Access/Jet Databases : Fields from Total Visual SourceBook

Add, edit, delete and retrieve files from Attachment fields by updating records in Access ACCDB database tables using DAO with VBA and VB6.

Procedure List

Procedure Name

Type

Description

(Declarations) Declarations Declarations and private variables for the modAttachmentFields module
IsFieldAttachment Procedure Checks if the specified field in a table in the specified database is of attachment type
GetAttachmentFieldFileNames Procedure Fill an array with an attachment field's list of file names
GetAttachmentTableFileNames Procedure List all the attachments of the specified attachment field in the first record of a table using this filter
GetAttachmentFileInfo Procedure List the information of all the attachments of the specified field in a table to the immediate window
PrintAttachmentFileInfo Procedure List the information of all the attachments of the specified field in a table to the immediate window
AddAttachmentToField Procedure Add one file into an Attachment field of a record that's in edit mode
AddAttachmentToRecords Procedure Add one file into an Attachment field. Loads the files for every record that matches the specified filter.
AddAttachmentsToRecords Procedure Loads multiple files into an Attachment field for every record specified
AddAttachmentsFromFolder Procedure Loads all files in a folder (directory) into an Attachment field. Loads the files for every record that matches the Where clause.
DeleteAttachmentsFromField Procedure Delete (remove) the specified attachment(s) from a field in one record
DeleteAttachments Procedure Removes the specified attachment(s) from a field across all the filtered records
ExportAttachmentsFromField Procedure Save (export) the files in the current attachment field to files in a specified folder (directory)
ExportAttachments Procedure Export (save) the files in an attachment field for all the records to a specified folder (directory)

Example Code for Using Module: AttachmentFields

' Example of modAttachmentFields
'
' To use this example, create a new module and paste this code into it.
' Then run either of the procedures by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

' This example assumes that this sample database exists, and contains the objects listed in the constants below.
Private Const mcstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"
Private Const mcstrSampleDB As String = mcstrSamplePath & "Sample.accdb"
Private Const mcstrSampleFile1 As String = "Test.txt"
Private Const mcstrSampleFile2 As String = "TestFile.txt"
Private Const mcstrTableName As String = "Customers-Complex"
Private Const mcstrAttachField As String = "Attachment"
Private Const mcstrIDField As String = "CustomerID"
Private Const mcstrFilter As String = "CustomerID = 'NWIND'"
Private Const mcstrFilterFMS As String = "CustomerID = 'FMS'"

Private Sub Example_modAttachmentFieldsInfo()
  ' Comments: Examples of using the modAttachmentFields module view (not modify) attachment fields in Microsoft Access ACCDB databases using VBA and VB6.
  '           See the results in the Immediate Window.

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim fld As DAO.Field
  Dim astrFileNames() As String
  Dim intCount As Integer
  Dim intCounter As Integer
  Dim strInfo As String

  Set dbs = OpenDatabase(mcstrSampleDB, False, False)
  Set rst = dbs.OpenRecordset(mcstrTableName, dbOpenDynaset, dbSeeChanges)
  Set fld = rst.Fields(mcstrAttachField)

  ' Test IsFieldAttachment
  If IsFieldAttachment(dbs, mcstrTableName, mcstrAttachField) Then
    Debug.Print "Field [" & mcstrAttachField & "] is an Attachment field."
  Else
    Debug.Print "Field [" & mcstrAttachField & "] is NOT an Attachment field."
  End If

  If IsFieldAttachment(dbs, mcstrTableName, mcstrIDField) Then
    Debug.Print "Field [" & mcstrIDField & "] is an Attachment field."
  Else
    Debug.Print "Field [" & mcstrIDField & "] is NOT an Attachment field."
  End If
  Debug.Print

  ' Get all the file names for each record's attachment field with an array; and go through all the records
  rst.MoveFirst
  Do Until rst.EOF
    intCount = GetAttachmentFieldFileNames(fld, astrFileNames)
    Debug.Print "GetAttachmentFieldFileNames returned " & intCount & " file names where " & mcstrIDField & "=" & rst.Fields(mcstrIDField)
    For intCounter = 1 To intCount
      Debug.Print "    - " & astrFileNames(intCounter)
    Next intCounter
    rst.MoveNext
  Loop
  Set fld = Nothing
  Debug.Print

  ' List all the attachments of the specified attachment field in the first record of a table using this filter
  intCount = GetAttachmentTableFileNames(dbs, mcstrTableName, mcstrAttachField, mcstrFilter, astrFileNames)
  Debug.Print "GetAttachmentTableFileNames returned " & intCount & " file names where " & mcstrFilter & ": "
  For intCounter = 1 To intCount
    Debug.Print "    - " & astrFileNames(intCounter)
  Next intCounter
  Debug.Print

  ' Retrieve the attachment file info in a string for all the records
  ' No filter
  Debug.Print "GetAttachmentFileInfo without filter"
  strInfo = GetAttachmentFileInfo(dbs, mcstrTableName, mcstrAttachField)
  Debug.Print strInfo

  ' Filtered to just one record
  Debug.Print "GetAttachmentFileInfo using filter " & mcstrFilterFMS
  strInfo = GetAttachmentFileInfo(dbs, mcstrTableName, mcstrAttachField, ";", mcstrFilterFMS)
  Debug.Print strInfo

  ' Get information on all the files in an Attachment field for a filtered set of records
  Debug.Print "PrintAttachmentFileInfo returns these property values where " & mcstrFilterFMS & ":"
  PrintAttachmentFileInfo dbs, mcstrTableName, mcstrAttachField, mcstrFilterFMS

  rst.Close
  Set rst = Nothing

  dbs.Close
  Set dbs = Nothing

End Sub

Private Sub Example_modAttachmentFieldsModify()
  ' Comments: Examples of using the modAttachmentFields module to add, delete, and export files to/from attachment fields in Microsoft Access ACCDB databases.
  '           See the results in the Immediate Window.

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim fld As DAO.Field
  Dim astrFileNames() As String
  Dim intCount As Integer

  Set dbs = OpenDatabase(mcstrSampleDB, False, False)
  Set rst = dbs.OpenRecordset(mcstrTableName, dbOpenDynaset, dbSeeChanges)
  Set fld = rst.Fields(mcstrAttachField)

  ' Save (export) the files in the current attachment field to files in a specified folder (directory)
  If ExportAttachmentsFromField(fld, mcstrSamplePath & "DestDir", True, "*.bmp") Then
    Debug.Print "Bitmap files exported from Record " & rst.Fields(mcstrIDField) & " to " & mcstrSamplePath & "DestDir"
  End If

  ' Export (save) the files in an attachment field for all the records to a specified folder (directory)
  If ExportAttachments(dbs, mcstrTableName, mcstrAttachField, mcstrSamplePath & "DestDir", True, mcstrFilter, "*.txt") Then
    Debug.Print "Text files exported to " & mcstrSamplePath & "DestDir where " & mcstrFilter & "."
  Else
    Debug.Print "Text files could not be exported to " & mcstrSamplePath & "DestDir"
  End If

  ' Add a file to an Attachment field
  rst.MoveFirst
  rst.Edit
  If AddAttachmentToField(fld, mcstrSamplePath & mcstrSampleFile1) Then
    Debug.Print mcstrSampleFile1 & " added to the first record."
  Else
    Debug.Print mcstrSampleFile1 & " could not be added to the first record."
  End If
  rst.Update

  ' Delete files from an Attachment field
  rst.MoveFirst
  rst.Edit
  If DeleteAttachmentsFromField(fld, mcstrSampleFile1) Then
    Debug.Print mcstrSampleFile1 & " removed from the first record."
  Else
    Debug.Print mcstrSampleFile1 & " could not be removed from the first record."
  End If
  rst.Update

  ' Add multiple files to an Attachment field
  Erase astrFileNames
  ReDim astrFileNames(0 To 1)
  astrFileNames(0) = mcstrSamplePath & mcstrSampleFile1
  astrFileNames(1) = mcstrSamplePath & mcstrSampleFile2
  If AddAttachmentsToRecords(astrFileNames, dbs, mcstrTableName, mcstrAttachField, mcstrFilter) Then
    Debug.Print "2 attachments added where " & mcstrFilter & "."
  Else
    Debug.Print "Attachments could not be added where " & mcstrFilter & "."
  End If

  ' Delete Attachments
  If DeleteAttachments(dbs, mcstrTableName, mcstrAttachField, mcstrFilter, mcstrSampleFile1) Then
    Debug.Print "Attachment '" & mcstrSampleFile1 & "' deleted where " & mcstrFilter & "."
  Else
    Debug.Print "Attachment '" & mcstrSampleFile1 & "' could not be deleted where " & mcstrFilter & "."
  End If

  rst.MoveFirst
  Do Until rst.EOF
    ' Delete (remove) the specified attachment file name from a field in the record
    If DeleteAttachmentsFromField(fld, mcstrSampleFile2) Then
      Debug.Print "Attachment '" & mcstrSampleFile2 & "' deleted."
    End If
    rst.MoveNext
  Loop
  Set fld = Nothing

  ' Import all files in a folder (directory) into an Attachment field.
  rst.AddNew
  rst.Fields(mcstrIDField) = "zzzzz"
  rst.Update
  intCount = AddAttachmentsFromFolder(mcstrSamplePath & "DestDir", dbs, mcstrTableName, mcstrAttachField, mcstrIDField & " = 'zzzzz'", "*.*")
  Debug.Print intCount & " attachemnts added where " & mcstrIDField & " = 'zzzzz'."

  ' Clean up by deleting the record
  rst.MoveLast
  rst.Delete

  rst.Close
  Set rst = Nothing

  dbs.Close
  Set dbs = Nothing

End Sub

Microsoft Access Module LibraryOverview of Total Visual SourceBook

The source code in Total Visual SourceBook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Additional Resources