在 MS Access 中通过 VBA 添加附件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14478860/
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-11 19:18:23  来源:igfitidea点击:

Add attachment via VBA in MS Access

databasevbams-accessaccess-vba

提问by Jay

One of my form is as follows

我的表格之一如下

enter image description here

在此处输入图片说明

It is a form made in MS Access which allows my users to add observations. An observation may include attachments as shown. I am able to click on attachment control and Add attachments to the popup that pops up. However, what is expected is that when I click ADD button as shown in the form above, this attachments shall be added to the corresponding field of a table.

它是一种在 MS Access 中制作的表单,它允许我的用户添加观察结果。观察可能包括如图所示的附件。我可以单击附件控件并将附件添加到弹出的弹出窗口中。但是,可以预期的是,当我单击上表所示的“添加”按钮时,应将此附件添加到表的相应字段中。

All controls on this form are unbound.

此表单上的所有控件均未绑定。

The code written behind ADD button is as follows:

添加按钮后面的代码如下:

Private Sub cmdAdd_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblObservation", dbOpenDynaset)

    rs.AddNew

    rs![Artifact] = artifactId
    rs![Observation Text] = txtObservationText.Value

    'rs![Attachments] = ' not able to solve this

    rs.Update
    rs.Close

    Set rs = Nothing
    Set db = Nothing
End Sub

回答by Jose R

You can use the following reference from Microsoft

您可以使用来自Microsoft的以下参考

The relevant piece of code is this:

相关的一段代码是这样的:

'  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