Working with Attachment Fields

, f.. ..:■■ y. , .jw-f rir ' The Attachment data type discussed in this section is new to Access 2007.

Previous versions of Access had an OLE Object field data type, which only supported certain types of objects and was quite cumbersome to use (not to mention causing terrible database bloat). By contrast, in Access 2007, it is quite easy to store files of any type in a field of the new Attachment data type, and the attachments are automatically compressed to save database space. The Contacts form, frmContactsWithAttachments (opened from the Browse Contacts button on the main menu) has such a field. If an attachment has already been added to the field, it shows as an icon (at least, if it is of a type recognized by Office); Figure 9.16 shows a record with a Word 2007 document attachment.

I i The attachment icons differ according to the Office version of the stored attachment pfi^KStksa&SiSBBaQS fi|e. \ Word document's icon has a Word document with a W image over the upper-left corner; the style of the W differs for Word 97-2003 (.doc) or Word 2007 (.docx) documents. For a Word 2007 document, the W is similar to the one displayed in a Word 2007 document's taskbar icon; for Word 97-2003 documents, the W is the older style W that was used as the Word icon in Office 97. Other Office documents also have different icons depending on their version.

I discussed using the FileSystemObject (Scripting Runtime library) for working with folders in an earlier section of this chapter; you can also use the FileSystemObject to work with files in a folder, or create files. One possible use is to select a file from a folder to store in a new Attachment data type field in an Access table.

FIGURE 9.16

A form with an attachment field, showing a stored Word 2007 document.

FIGURE 9.16

A form with an attachment field, showing a stored Word 2007 document.

To add an attachment, just double-click the field; this opens the Attachments dialog (shown in Figure 9.17), where you can view existing attachments (multiple attachments can be stored in one Attachment field), or add a new one by clicking the Add button.

FIGURE 9.17

The Attachment dialog, showing a stored Word 2007 document.

FIGURE 9.17

The Add button in the Attachments dialog (shown in Figure 9.17) opens a Choose File dialog (shown in Figure 9.18) where you can select a file to store in the Attachment field.

Selecting a TIF file in the Choose File dialog.

Choose File

Ua-I

, « Contact Documents

■w W 1

.t. | (®| Search

^ Organize ▼ l|| Views ▼ New Folder

Favorite Links ^ Default Templates J?] Documents Jl Custom Templates J^j Knitting Patterns

Recent Pisces W_ Desktop jlfl Computer ^ Download Unzip

Office Book More »

Favorite Links ^ Default Templates J?] Documents Jl Custom Templates J^j Knitting Patterns

Recent Pisces W_ Desktop jlfl Computer ^ Download Unzip

Office Book More »

Folders

¡Ü] Avery 5160 Labels on 2-18-2007.doc ^ Avery 5160 Labels on 2-18-2007.dooc i^] Avery 5160 Merge Labels, doc

Avery 5161 Labels 2 on 2-18-2007.doc* Avery 5161 Labels on 2-18-2007.doot @ Avery 5162 Labels 2 on 2-18 2007 doot Avery 5162 Labels on 2-18-2007.dooc Contact ID 7 Letter on 7-7-2006.doc Contact ID 10 Letter on 7-7-2006.do< Contact ID 12 Letter on 7-7-2006.doc Contact ID 15 Letter 4 on 7-7-2006, doc Contact ID 28 Letter on 7-7-2006.doc Contact ID 30 Letter on 7-8-2006.dooc Contact ID 42 Letter on 7-8-2006.dot* W] Contact ID 75 Letter on 7-8-2006.dotx fc*li Contact ID 81 Letter on 7-8-2006.doot < l " -1

Date modified 2/18/2007 3:38 PM 2/18/2007 4:45 PM 7/7/20061:04 PM 2/18/2007 4:49 PM 2/18/2007 5:20 PM 2/18/2007 4:50 PM 2/18/2007 3:42 PM 7/7/2006 11:00 AM 7/7/200611:28 AM 7/7/200611:33 AM 7/7/200611:46 AM 7/7/200611:13 AM 7/8/200610:15 AM 7/8/200610:24 AM 7/8/200610:34 AM 7/8/200610:35 AM

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 attachments 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.

0 0

Post a comment