【问题标题】:Attach file in MS Access using the path in the same record使用同一记录中的路径在 MS Access 中附加文件
【发布时间】:2015-08-09 03:34:44
【问题描述】:

我在 MS Access 方面有一些经验,但似乎找不到将文件附加到记录的 vba。我只有大约 2,000 条记录(不再有,因为它是一个数据输入项目),所以 MS Access 数据库大小不会成为问题。每条记录在“路径”字段中都有完整路径,但我不知道如何使用 vba 将文件附加到新的“附件”字段中。

到目前为止,我所看到的一切都是关于用户附加文件或创建路径 - 不是实际使用 vba 附加文件(假设我已经有了路径)。

任何帮助将不胜感激!

【问题讨论】:

    标签: ms-access vba ms-access-2010


    【解决方案1】:

    您需要使用 Access DAO.Field2 对象的 .LoadFromFile 方法。例如,给定一个名为 [tblTest] 的表

    ID - 自动编号,主键
    MyFilePath - 文本 (255)
    MyFileAttach - 附件

    包含

    ID  MyFilePath                      MyFileAttach
    --  ------------------------------  ------------
    1   C:\Users\Gord\Pictures\r.bmp    @(0)
    2   C:\Users\Gord\Pictures\g.bmp    @(0)
    3   C:\Users\Gord\Pictures\b.bmp    @(0)
    

    以下代码会将指定文件插入到每条记录的 [MyFileAttach] 字段中

    Dim cdb As DAO.Database, rstMain As DAO.Recordset, rstAttach As DAO.Recordset2, _
            fldAttach As DAO.Field2
    Set cdb = CurrentDb
    Set rstMain = cdb.OpenRecordset("SELECT MyFilePath, MyFileAttach FROM tblTest", dbOpenDynaset)
    Do Until rstMain.EOF
        rstMain.Edit
        Set rstAttach = rstMain("MyFileAttach").Value
        rstAttach.AddNew
        Set fldAttach = rstAttach.Fields("FileData")
        fldAttach.LoadFromFile rstMain("MyFilePath").Value
        rstAttach.Update
        rstAttach.Close
        Set rstAttach = Nothing
        rstMain.Update
        rstMain.MoveNext
    Loop
    rstMain.Close
    Set rstMain = Nothing
    Set cdb = Nothing
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2015-09-26
      • 1970-01-01
      • 2016-12-30
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多