شكرا استاذ موسى على المرفق الجميل جزاك الله خيرا
قمت باستلال الزبدة منه .. لكونها هنا ابقى واقرب للتناول
On Error GoTo errHandler
Dim rs1 As DAO.Recordset 'Table with attachments to be imported
Dim rs2 As DAO.Recordset 'Table to import attachments into
Dim rs3 As DAO.Recordset2 'Attachments to be imported
Dim rs4 As DAO.Recordset2 'Recordset to accept the new attachments
Dim strSQL As String
'Open table with attachments
strSQL = "SELECT RecordID, Attachments FROM tblOldTable WHERE Attachments.FileName Is Not Null ORDER BY RecordID"
Set rs1 = db.OpenRecordset(strSQL, dbOpenSnapshot)
'Loop through all the records to be imported
Do While Not rs1.EOF
'Open table to be appended
strSQL = "SELECT RecordID, Attachments FROM tblNewTable WHERE RecordID=" & rs1!recordid
Set rs2 = db.OpenRecordset(strSQL, dbOpenDynaset)
'Recordsets for the attachment fields
Set rs3 = rs1!Attachments.Value
Set rs4 = rs2!Attachments.Value
'Table to be appended must be in edit mode
rs2.Edit
'Add all new attachments (Note: Access automatically adds the file type)
Do While Not rs3.EOF
rs4.AddNew
rs4!FileData = rs3!FileData
rs4!FileName = rs3!FileName
rs4.Update
rs3.MoveNext
Loop
'Update parent record
rs2.Update
'Go to next record with attachment to import
rs1.MoveNext
Loop
'Refresh new table subform
Me.frmNewAttachment.Requery
errExit:
'Cleanup
rs2.Close
rs1.Close
Set rs4 = Nothing
Set rs3 = Nothing
Set rs2 = Nothing
Set rs1 = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
Resume errExit