I'm creating a report in which I want to display all attached files for each record.
My problem is that the report only display the first file instead of all the files.
Many of us here do not use attachment fields, but that is another story perhaps. As a result, I have to ask, you can actually have multiple attachments for any record? I would not have thought so but that is how your question reads.
if all your Attachments are Images (pictures), then use a Query as recordsource of your report:
select field1, attachmentFieldName.Filename as Filename from yourTable;
on your report, remove the attachment control and put an Image Control.
add code to Detail section (Format event) of report:
Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim d As DAO.Database
Dim a As DAO.Recordset2
Dim b As DAO.Recordset2
Dim p As String
p = Environ("temp") & "\" & [FileName]
Set d = CurrentDb
Set a = d.OpenRecordset("select * from tblAttachment where id=" & [ID] & ";")
Set b = a.Fields("attachmentFieldNameHere").Value
With b
.MoveFirst
While .Fields("Filename") <> [FileName]
.MoveNext
Wend
If Dir(p) <> "" Then Kill p
.Fields("FileData").SaveToFile p
End With
Set b = Nothing
Set a = Nothing
Set d = Nothing
[yourImageControl].Picture = p
End Sub