简体   繁体   中英

How do I save all the attachments in a single field for all records in a MS Access table?

I am working with a Microsoft Access 2010 database.

I have a number of pictures saved as attachments in one field of a table.

How do I save all the pictures to a local folder for all records simultaneously?

I need to preserve the names of the attachments but they can all go in the same folder.

Thank you :-)

Use below sub to save all attachment file for all records.

Private Sub cmdLoadPicture_Click()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2
Dim savePath As String

    savePath = "C:\Users\Harun.Rashid\Pictures\Screenshots\" 'Folder path to save files

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblEmpInfo") 'tblEmpInfo is table name
    Set fld = rst("EmpPhoto") 'EmpPhoto is Field name to table with attachment data type.

    Do While Not rst.EOF 'Loop through all records of table.
    Set rsA = fld.Value
        On Error Resume Next 'Omit errors if file already exist
        Do While Not rsA.EOF 'Loop through all attachment of single record
            rsA.Fields("FileData").SaveToFile savePath 'Save file to disk
            rsA.MoveNext
        Loop
    rst.MoveNext
    Loop

    rst.Close
    dbs.Close
Set fld = Nothing
Set rst = Nothing
Set dbs = Nothing
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM