Microsoft Access数据库-从多个条目下载所有附件

时间:2018-08-13 19:33:51

标签: ms-access

我当前正在尝试下载许多附加到Microsoft Access记录的“ .doc”文件,该文件位于“附件”字段下,该字段被标记为“文件/附件”。但是,我需要能够运行之前执行的查询(按损失事件搜索),然后运行宏以从多个记录中下载所有附件。这是下面的代码,我需要一些帮助!我收到错误消息“您输入的此表达式的函数包含错误数量的参数”。

Option Compare Database

Public Function SaveAttachmentsTest(strPath As String, Optional strPattern As String = "*.*") As Long
Dim dbs As DAO.database
Dim rst As DAO.Recordset
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2
Dim strFullPath As String

'Get the database, recordset, and attachment field
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Search By Loss Incident Name")
Set fld = rst("File/Attachment")

'Navigate through the table
Do While Not rst.EOF

'Get the recordset for the Attachments field
Set rsA = fld.Value

'Save all attachments in the field
Do While Not rsA.EOF
If rsA("FileName") Like strPattern Then
'To Export the data, use the line below
strFullPath = "C:\Users\Emmanuel\Desktop\Test" & "\" & rsA("FileName")

'Make sure the file does not exist and save
If Dir(strFullPath) = "" Then
rsA("FileData").SaveToFile strFullPath
End If

'Increment the number of files saved
SaveAttachmentsTest = SaveAttachmentsTest + 1
End If

'Next attachment
rsA.MoveNext
Loop
rsA.Close

'Next record
rst.MoveNext
Loop

rst.Close
dbs.Close

Set fld = Nothing
Set rsA = Nothing
Set rst = Nothing
Set dbs = Nothing
End Function

1 个答案:

答案 0 :(得分:0)

引入一个局部变量

<snip>

    Dim SavedAttachments As Long

<snip>

    ' Increment the number of files saved.
    SaveAttachments = SaveAttachments + 1
End If

<snip>

    Set fld = Nothing
    Set rsA = Nothing
    Set rst = Nothing
    Set dbs = Nothing

    ' Return the count of saved files.
    SaveAttachmentsTest = SaveAttachments

End Function