我正在尝试将一条记录复制到同一个表中的另一条记录。该表链接到 SharePoint 列表。我曾尝试将 DAO、ADO、AppendChunk、SharePoint 列表 GUID 与 ADODB 连接一起使用,但均未成功。我关注了以下网站,但仍然没有成功:DAO, 大块, ADODB.
当代码到达行 .MoveNext 时,附件字段的 .RecordCount 跳到 2,000 多个。好像要遍历整个表格,把所有的附件都复制过来。
我创建了一个临时的本地测试数据库来隔离问题出在哪里。我创建了 4 个字段(ID [AutoNumber]、FirstName [Short Text]、LastName [Short Text]、Document [Attachment])。我用数据填充了 3 条记录,复制了第一条记录,并成功地将其粘贴为新记录。然后我将该表发送到 SharePoint 并从 Access 链接到它。但是复制粘贴是行不通的,需要先提交记录,再处理附件。我的代码如下。
Public Sub CopyAttachments(copyFrom As DAO.Recordset2, copyTo As DAO.Recordset2, fromAttachment As String, toAttachment As String)
Dim objFromAttachment As DAO.Recordset2
Dim objToAttachment As DAO.Recordset2
Dim objFromField As DAO.Field2
Dim strFullFileName As String
Dim strSaveAs As String
Dim objFSO As FileSystemObject
Set objFSO = New FileSystemObject
Set objFromAttachment = copyFrom.Fields(fromAttachment).Value
Set objToAttachment = copyTo.Fields(toAttachment).Value
copyTo.Edit
With objFromAttachment
If Not (.BOF And .EOF) Then
.MoveFirst
Do While Not .EOF
' 'Get file name to save as
strSaveAs = GetAttachmentName(.Fields("FileName").Value)
'
' Set the field to the file data
Set objFromField = .Fields("FileData")
' Delete file if it already exists
If objFSO.fileExists(strSaveAs) Then
objFSO.DeleteFile strSaveAs
End If
objFromField.SaveToFile strSaveAs
' Add a new child record
objToAttachment.AddNew
' Load the file as a new attachment
objToAttachment.Fields("FileData").LoadFromFile strSaveAs
' Update child record
objToAttachment.Update
'Delete file
objFSO.DeleteFile strSaveAs
'' Move to the next attachment
.MoveNext 'Here is where the attachment count increases!!
Loop
End If
End With
copyTo.Update
Set objFromAttachment = Nothing
Set objToAttachment = Nothing
End Sub
Private Function GetAttachmentName(FileNameValue As String) As String
Dim varTemp As Variant
Dim strTemp As String
varTemp = Split(FileNameValue, "/")
strTemp = Environ("UserProfile") & "\" & varTemp(UBound(varTemp))
GetAttachmentName = strTemp
End Function
下面是我从测试数据库中得到的。
When copying record 1 to record 6
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File One.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File Two.xlsm
When copying record 2 to record 6
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/2/File Three.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File Two.xlsm
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/2/File Three.pdf
When copying record 3 to record 6
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/3/File Four.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File Two.xlsm
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/2/File Three.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/3/File Four.pdf
When copying record 4 to record 6
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/4/File Five.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File Two.xlsm
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/2/File Three.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/3/File Four.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/4/File Five.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/4/File Six.pdf
When copying record 5 to record 6
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/5/File Seven.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/1/File Two.xlsm
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/2/File Three.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/3/File Four.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/4/File Five.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/4/File Six.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/5/File Seven.pdf
https://sharepointsite.com/sites/TestSite/TestName/Lists/tblTest/Attachments/5/File Eight.xlsm
所以我的问题是为什么明明只有1个附件,附件数量却增加到数千?感谢您提供的任何帮助。