How to auto attach images in Access 2010 using vba/macros?

前端 未结 3 952
青春惊慌失措
青春惊慌失措 2021-01-16 04:13

I have a table where there is a \"Photo\" text field with the name of the file. I also have the actual files in a separate folder. I want to attach those files to the databa

3条回答
  •  孤街浪徒
    2021-01-16 04:53

    Attachments are quite different from OLE objects. The first should be compacted and are managed without OLE servers installed on machine. For example, when you add a OLE object to a MS-Access field, this object is transformed in a kind of bitmap, which ought to be very large. In attachment fields, several file formats are automatically compacted on database. Also, you are able to import more than just only one file. In this case, Access does, behind the scenes, relational database model for improving efficiency.

    You should load and save file formats in attachment fields as follows:

    '  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
    
    '  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
    

    for more information, visit http://msdn.microsoft.com/pt-br/library/bb258184%28v=office.12%29.aspx

提交回复
热议问题