Solved Export All Attachments From a table to Folders (1 Viewer)

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
Hi,
I want to export all attachments from a table, where multiple records are saved with their multiple attachments, i mean a record has multiple attachments.
i want to design a code which can works like below.

1. Create a folder with Product_ID name (like 355, 317)
2. Save all attachments of 355 in 355 name folder
below is image for your reference. please help me to complete this code.

1665933626478.png
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:49
Joined
Feb 19, 2013
Messages
16,617
take a look at thread - basically exactly the same question
 

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
take a look at thread - basically exactly the same question
i have tried this code but it is not working.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:49
Joined
Sep 21, 2011
Messages
14,311
i have tried this code but it is not working.
So show your code and say EXACTLY what is not working.
Have you even walked though the code, line by line to see what happens? :(

Just saying *not working* is not helpful in the least:(
 

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
So show your code and say EXACTLY what is not working.
Have you even walked though the code, line by line to see what happens? :(

Just saying *not working* is not helpful in the least:(
Sorry, i didn't get the output on that path where i exactly located to found output of the code, but when i saw documents folder of my PC i found all the attachments were saved. But the file name and folder name was incorrect according to what i want to do. here is a snap shot.

1665940966583.png


i have created a module and pasted all the code. and i m using a command button to run this code. i want to save all attachments with their Product_ID code named folder in below mention path.

Private Sub Command0_Click()
'
' MODIFY THIS LINE
'
SaveAttachmentsTest ("C:\Users\Atiq\Desktop\SavedAttachments\")
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:49
Joined
Feb 19, 2013
Messages
16,617
As gasman suggests, step through the code, find where the documents path is assigned and change it to what you want
 

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
As gasman suggests, step through the code, find where the documents path is assigned and change it to what you want
here is the code there is no path to assign

Option Compare Database
Option Explicit
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 rsB As String
Dim fld As DAO.Field2
Dim OrdID As DAO.Field2
Dim strFullPath As String
Dim thisPath As String
'Get the database, recordset, and attachment field
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("CertificatesTable")
Set fld = rst("Upload_Document")
Set OrdID = rst("Product_ID")

'Navigate through the table
Do While Not rst.EOF

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

'Save all attachments in the field
Do While Not rsA.EOF
Set OrdID = rst("Certification_ID")
If rsA("FileName") Like strPattern Then
'To Export the data, use the line below
thisPath = Replace(strPath & "" & OrdID & "", "\", "")
subMakePath thisPath
strFullPath = thisPath & 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

Private Sub subMakePath(NewPath As String)
Dim var, direc
Dim sPath As String
var = Split(NewPath, "")
For Each direc In var
sPath = sPath & direc & ""
If Dir(sPath, vbDirectory) = "" Then
MkDir sPath
End If
Next
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:49
Joined
Sep 21, 2011
Messages
14,311
Well from your picture, you have lost the last \
So you walk through your code after setting a breakpoint and see where that happens.
Likely your Replace statement
 

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
Well from your picture, you have lost the last \
So you walk through your code after setting a breakpoint and see where that happens.
Likely your Replace statement
Could you please help me to solve this issue i didn't get the error. file is attached.
 

Attachments

  • TestDatabase.accdb
    1.8 MB · Views: 108

Gasman

Enthusiastic Amateur
Local time
Today, 14:49
Joined
Sep 21, 2011
Messages
14,311
In fact you lost ALL your \ characters with that Replace.? :(
Look at your Replace statement, that is the cause of all your woes.
 
Last edited:

atiqahmed

New member
Local time
Today, 18:49
Joined
Mar 17, 2021
Messages
22
In fact you lost ALL your \ characters with tha Replace.? :(
Look at your Replace statement, that is the cause of all your woes.
thank you so much for your hint. i have found the error, now its working perfectly...
thank you so much
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:49
Joined
Sep 21, 2011
Messages
14,311
thank you so much for your hint. i have found the error, now its working perfectly...
thank you so much
Always better to fix it yourself, that way you learn something.
People handing you fixed code, tends not to get learnt. :(
 

Users who are viewing this thread

Top Bottom