如何使用 vba/宏在 Access 2010 中自动附加图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4787149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:05:26  来源:igfitidea点击:

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

ms-accessvba

提问by Alex

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 database, and not keep them in a separate folder. So I've created a separate "Picture" attachment field. But I don't know how I can attach those files to this field automatically. Can you give me some directions?

我有一个表格,其中有一个带有文件名称的“照片”文本字段。我也有一个单独的文件夹中的实际文件。我想将这些文件附加到数据库中,而不是将它们保存在单独的文件夹中。所以我创建了一个单独的“图片”附件字段。但我不知道如何将这些文件自动附加到该字段。你能给我一些方向吗?

回答by Alex

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.

附件与 OLE 对象完全不同。第一个应该被压缩并在没有安装在机器上的 OLE 服务器的情况下进行管理。例如,当您将 OLE 对象添加到 MS-Access 字段时,该对象将转换为一种位图,该位图应该非常大。在附件字段中,多种文件格式会自动压缩到数据库中。此外,您可以导入多个文件。在这种情况下,Access 在幕后使用关系数据库模型来提高效率。

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

有关详细信息,请访问http://msdn.microsoft.com/pt-br/library/bb258184%28v=office.12%29.aspx

回答by Fubzot

This helped me:

这帮助了我:

Originaly posted by HiTechCoach on http://www.access-programmers.co.uk/forums/showthread.php?t=169056

最初由 HiTechCoach 发布在 http://www.access-programmers.co.uk/forums/showthread.php?t=169056

On Error GoTo Err_AddImage

Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2

Set db = CurrentDb
Set rsParent = Me.Recordset

rsParent.Edit

Set rsChild = rsParent.Fields("AttachmentTest").Value

rsChild.AddNew
rsChild.Fields("FileData").LoadFromFile ("c:\Sunset.jpg")

rsChild.Update
rsParent.Update

Exit_AddImage:

Set rsChild = Nothing
Set rsParent = Nothing
Exit Sub

Err_AddImage:

If Err = 3820 Then
MsgBox ("File already part of the multi-valued field!")
Resume Next

Else
MsgBox "Some Other Error occured!", Err.Number, Err.Description
Resume Exit_AddImage

End If

回答by user12263349

On Error GoTo Err_AddImage Dim db As DAO.Database Dim rsParent As DAO.Recordset2 Dim rsChild As DAO.Recordset2 Set db = CurrentDb Set rsParent = Me.Recordset rsParent.Edit Set rsChild = rsParent.Fields("AttachmentTest").Value rsChild.AddNew rsChild.Fields("FileData").LoadFromFile ("c:\Sunset.jpg") rsChild.Update rsParent.Update Exit_AddImage: Set rsChild = Nothing Set rsParent = Nothing Exit Sub Err_AddImage: If Err = 3820 Then MsgBox ("File already part of the multi-valued field!") Resume Next Else MsgBox "Some Other Error occured!", Err.Number, Err.Description Resume Exit_AddImage End If

On Error GoTo Err_AddImage Dim db As DAO.Database Dim rsParent As DAO.Recordset2 Dim rsChild As DAO.Recordset2 Set db = CurrentDb Set rsParent = Me.Recordset rsParent.Edit Set rsChild = rsParent.Fields("AttachmentTest").Value rsChild。 AddNew rsChild.Fields("FileData").LoadFromFile("c:\Sunset.jpg") rsChild.Update rsParent.Update Exit_AddImage: Set rsChild = Nothing Set rsParent = Nothing Exit Sub Err_AddImage: If Err = 3820 Then MsgBox ("File已经是多值字段的一部分!") 继续下一个 Else MsgBox "发生了其他一些错误!", Err.Number, Err.Description Resume Exit_AddImage End If