我有一个表格,其中有一个带有文件名称的“Photo”文本字段。我还将实际文件放在一个单独的文件夹中。我想将这些文件附加到数据库,而不是将它们保存在单独的文件夹中。所以我创建了一个单独的“图片”附件字段。但我不知道如何自动将这些文件附加到此字段。你能给我一些指示吗?
答案 0 :(得分:1)
附件与OLE对象完全不同。第一个应该被压缩并且在没有安装在机器上的OLE服务器的情况下进行管理例如,当您将一个OLE对象添加到MS-Access字段时,此对象将转换为一种位图,应该非常大。在附件字段中,会自动在数据库上压缩多种文件格式。此外,您还可以导入多个文件。在这种情况下,Access在后台执行关系数据库模型以提高效率。
您应该在附件字段中加载和保存文件格式,如下所示:
' 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
有关详细信息,请访问http://msdn.microsoft.com/pt-br/library/bb258184%28v=office.12%29.aspx
答案 1 :(得分:0)
这对我有所帮助:
最初由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
答案 2 :(得分:0)
在出错时,转到Torr_AddImage Dim db作为DAO.Database Dim rsParent作为DAO.Recordset2 Dim rsChild作为DAO.Recordset2设置db = CurrentDb设置rsParent = Me.Recordset rsParent.Edit设置rsChild = rsParent.Fields(“ AttachmentTest”)。值rsChild.AddNew rsChild.Fields(“ FileData”)。LoadFromFile(“ c:\ Sunset.jpg”)rsChild.Update rsParent.Update Exit_AddImage:设置rsChild =否设置rsParent =否退出子Err_AddImage:如果Err = 3820,则MsgBox (“文件已成为多值字段的一部分!”)恢复下一个其他MsgBox“发生了其他错误!”,Err.Number,Err.Description继续Exit_AddImage如果结束