【问题标题】:Concatenate ID field to filename将 ID 字段连接到文件名
【发布时间】:2021-11-26 07:42:33
【问题描述】:

在 MS Access 中,我想用 ID 和文件名重命名附件的文件名,以便重复出现问题。例如,如果 id 是 1,文件名是 ABC,那么文件夹中的名称应该是 1ABC 或 1_ABC 什么都可以。目前保存为 ABC.extension (pdf/docx/txt)。

【问题讨论】:

  • 出现错误时该行的变量值是多少?
  • 在 MS Access 表中,ID 是包含从 1 到 178 的值的字段,第一行是 1,第二行是 2。我想从 ID 字段中获取 ID 值并在保存时将其连接到文件名附件。在 Dubug 中,它要求我分配值 ID = 1。但我想通过程序从表字段中获取值。
  • 你没有rsB ...你的代码无法编译。

标签: vba ms-access


【解决方案1】:

试试这个。

Private Sub Command0_Click()

    Dim counter As Long
    counter = SaveAttachments("D:\Test1")

    MsgBox counter & " files exported."

End Sub

Public Function SaveAttachments(savePath As String, Optional strPattern As String = "*.*") As Long

    Dim r As DAO.Recordset
    Dim r2 As DAO.Recordset2
    Dim strFullPath As String
    Dim counter As Long
    
    Set r = CurrentDb().OpenRecordset("Notices")

    Do While Not r.EOF

        Set r2 = r("Attachments").Value

        Do While Not r2.EOF

            If r2("FileName") Like strPattern Then
                strFullPath = savePath & "\" & r("ID") & "_" & r2("FileName")

                If Dir(strFullPath) = "" Then
                    r2("FileData").SaveToFile strFullPath
                    counter = counter + 1
                End If
            End If

            r2.MoveNext
        Loop

        If Not r2 Is Nothing Then r2.Close
        r.MoveNext
    Loop

    If Not r Is Nothing Then r.Close

    SaveAttachments = counter
End Function

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2023-03-21
    • 1970-01-01
    • 2020-07-16
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2023-03-05
    相关资源
    最近更新 更多