FIGURE 9.18
Selecting a TIF file in the Choose File dialog.
If you store multiple files in an Attachment field, only the first file’s icon will be displayed
in the form control.
Though it is easy enough to store an attachment or two manually, if you have a folder full of attach-
ments that need to be stored in hundreds of records, it is easier to use VBA code to store the
attachments, or to extract attachments and save them to a folder. As an example, suppose you have
a folder containing numerous Word documents and Excel worksheets (both in Office 2007 and
earlier formats) related to contacts. Each document name starts with “Contact ID” and a number,
which corresponds to the ContactID field in tblContacts in the sample database.
Loading Files into Attachment Fields ......................................................................
The Recordset2 object (new to Access 2007) is used to work with fields of the
Attachment type.
The LoadAttachmentsprocedure listed next iterates through the documents in the folder
selected by the Input Documents Path button on the main menu, and for any document that starts
with “Contact ID” saves the document to the corresponding contact record’s File field (this field is of
the Attachment data type). An Attachment field can contain multiple attachments, and the collection
of attachments is represented in VBA code as a separate recordset of attachments belonging to a
record in a table. Using a Recordset2object (new to Access 2007) to work with the attachments
lets you use the new LoadFromFileand SaveToFilemethods to work with the attachments.
Figure 9.19 shows a folder with Contact ID documents of various types for loading as attachments.
NEW FEATURENEW FEATURE
NOTENOTE
Working with Files and Folders 9