VBA 附件:在集合中找不到项目
VBA Attachment: Item Not found in Collection
全部,
我正在尝试将 1 条记录的记录保存到驱动器。我花了大约一天的时间寻找解决方案,所以这是寻求帮助的最后努力。无论如何我都不是开发人员,所以请放轻松。
代码如下。
Table记录所在位置:tracker.
我搜索的字段基于:ReqID - 其中 ReqID = 我正在输入的记录,找到附件并将其移动到某个位置。
Dim db As DAO.Database
Dim rsChild As DAO.Recordset2
Dim ReqID As String
ReqID = Me.Form![Text145]
Debug.Print ReqID
Set db = CurrentDb
Set rsChild = db.OpenRecordset("Select * from tracker Where " & ReqID & " = [tracker].[ID]", dbOpenDynaset)
Debug.Print rsChild.RecordCount
If (rsChild.EOF = False) Or (rsChild.BOF = False) Then
While Not rsChild.EOF
rsChild("FileData").SaveToFile "C:\Users\<folder>\"
rsChild.Delete
Wend
End If
您实际上需要使用两个 Recordset 对象:一个用于主记录,另一个用于与该记录关联的附件。这是对我有用的示例代码,其中 [tblTest] 是 table 的名称,[Attachments] 是 Attachment
字段的名称:
Option Compare Database
Option Explicit
Sub SaveAllAttachments()
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim rstMain As DAO.Recordset
Set rstMain = cdb.OpenRecordset("SELECT Attachments FROM tblTest WHERE ID=1", dbOpenDynaset)
rstMain.Edit
Dim rstChild As DAO.Recordset2
Set rstChild = rstMain.Fields("Attachments").Value
Do Until rstChild.EOF
Dim fileName As String
fileName = rstChild.Fields("FileName").Value
Debug.Print fileName
Dim fld As DAO.Field2
Set fld = rstChild.Fields("FileData")
fld.SaveToFile "C:\Users\Gord\Desktop\" & fileName
rstChild.Delete ' remove the attachment
rstChild.MoveNext
Loop
rstChild.Close
rstMain.Update
rstMain.Close
End Sub
全部,
我正在尝试将 1 条记录的记录保存到驱动器。我花了大约一天的时间寻找解决方案,所以这是寻求帮助的最后努力。无论如何我都不是开发人员,所以请放轻松。
代码如下。
Table记录所在位置:tracker.
我搜索的字段基于:ReqID - 其中 ReqID = 我正在输入的记录,找到附件并将其移动到某个位置。
Dim db As DAO.Database
Dim rsChild As DAO.Recordset2
Dim ReqID As String
ReqID = Me.Form![Text145]
Debug.Print ReqID
Set db = CurrentDb
Set rsChild = db.OpenRecordset("Select * from tracker Where " & ReqID & " = [tracker].[ID]", dbOpenDynaset)
Debug.Print rsChild.RecordCount
If (rsChild.EOF = False) Or (rsChild.BOF = False) Then
While Not rsChild.EOF
rsChild("FileData").SaveToFile "C:\Users\<folder>\"
rsChild.Delete
Wend
End If
您实际上需要使用两个 Recordset 对象:一个用于主记录,另一个用于与该记录关联的附件。这是对我有用的示例代码,其中 [tblTest] 是 table 的名称,[Attachments] 是 Attachment
字段的名称:
Option Compare Database
Option Explicit
Sub SaveAllAttachments()
Dim cdb As DAO.Database
Set cdb = CurrentDb
Dim rstMain As DAO.Recordset
Set rstMain = cdb.OpenRecordset("SELECT Attachments FROM tblTest WHERE ID=1", dbOpenDynaset)
rstMain.Edit
Dim rstChild As DAO.Recordset2
Set rstChild = rstMain.Fields("Attachments").Value
Do Until rstChild.EOF
Dim fileName As String
fileName = rstChild.Fields("FileName").Value
Debug.Print fileName
Dim fld As DAO.Field2
Set fld = rstChild.Fields("FileData")
fld.SaveToFile "C:\Users\Gord\Desktop\" & fileName
rstChild.Delete ' remove the attachment
rstChild.MoveNext
Loop
rstChild.Close
rstMain.Update
rstMain.Close
End Sub