Export (0) Print
Expand All
Expand Minimize
8 out of 15 rated this helpful - Rate this topic

How to: Work With Attachments In DAO

Office 2007

In DAO, Attachment fields function just like other multi-valued fields. The field that contains the attachment contains a recordset that is a child to the table's recordset. There are two new DAO methods, LoadFromFile and SaveToFile, that deal exclusively with attachments.

Add an Attachment to a Record

The LoadFromFile method loads a file from disk and adds the file as an attachment to the specified record. The syntax of the LoadFromFile method is as follows:

Recordset.Fields("FileData").LoadFromFile(<filename>)
Bb258184.vs_note(en-us,office.12).gif  Note
The FileData field is reserved internally by the Access database engine to store the binary attachment data.

The following code snippet uses the LoadFromFile method to load an employee's picture from disk.

   '  Instantiate the parent recordset. 
   Set rsEmployees = db.OpenRecordset("Employees")
 
   … Code to move to desired employee
 
   ' Activate edit mode.
   rsEmployees.Edit
 
   ' Instantiate the child recordset.
   Set rsPictures = rsEmployees.Fields("Pictures").Value 
 
   ' Add a new attachment.
   rsPictures.AddNew
   rsPictures.Fields("FileData").LoadFromFile "EmpPhoto39392.jpg"
   rsPictures.Update
 
   ' Update the parent record
   rsEmployees.Update

Save an Attachment to Disk

The following code snippet illustrates how to use the SaveToFile method to save all of the attachments for a specific employee to disk.

'  Instantiate the parent recordset. 
   Set rsEmployees = db.OpenRecordset("Employees")
 
   … Code to move to desired employee
 
   ' Instantiate the child recordset.
   Set rsPictures = rsEmployees.Fields("Pictures").Value 

   '  Loop through the attachments.
   While Not rsPictures.EOF
 
      '  Save current attachment to disk in the "My Documents" folder.
      rsPictures.Fields("FileData").SaveToFile _
                  "C:\Documents and Settings\Username\My Documents"
      rsPictures.MoveNext
   Wend


Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.