仅在 Access 报告中查看最近添加的附件
View only most recently added attachment in Access report
我正在使用访问数据库作为我们游泳池的会员管理应用程序。我使用报告打印出所有带有照片 ID 的季票。该报告使用一个查询来仅为已拍照的顾客打印通行证。
SELECT tblPassHolders.[PASS HOLDER NAME], tblPassHolders.PHOTO.FileData, tblPassHolders.BARCODE, tblPassHolders.[FAMILY PASS], tblFamilyPass.Expires
FROM tblFamilyPass INNER JOIN tblPassHolders ON tblFamilyPass.ID = tblPassHolders.FamilyID
WHERE (((tblPassHolders.PHOTO.FileData) Is Not Null) AND ((tblFamilyPass.Expires)>Now()) AND ((tblPassHolders.Printed)=False));
这是使用该应用程序的第二年,我遇到的问题是当人们为他们的通行证重新拍照时,然后我的报告打印出 每个人[=25 的通行证=] 照片附件附在他们的记录上。
我对 Access 仍然很业余,我正在努力弄清楚如何编辑我的 report/query 以便报告只打印 one pass for每个赞助人使用附在他们记录上的最新照片。一种解决方案是简单地删除旧照片,这样只有一个附件,但我想弄清楚即使有多个附件,如何让它工作。我一直在玩 DLast()
但我很确定我做错了。
如何进行只显示最近添加的附件的查询?
这是我建议您可以执行的操作的示例,而不是查找最近添加的附件。
如前所述,除非您实施某种命名约定,将最新文件弹出到顶部,否则这是不可能的,因为 Microsoft 按文件名顺序显示附件字段。
这是一个示例,您可以使用按钮来控制附件的插入。
它基于 Stack Overflow example
完成测试后,您可以将 Cancel=True
添加到 Photo_DblClick
事件以完全控制您的照片附件字段
Private Sub cmdAddNewPhoto_Click()
Dim rsPhotos As DAO.Recordset2
Dim rsParent As DAO.Recordset2
Dim strImagePath As String
If MsgBox("Add New Photo?", vbQuestion + vbOKCancel, "Add Photo?") = vbOK Then
' Get New Photo
' Note that you need to add a reference to Microsoft Office ##.0 Object Library
' using Tools | References... from the VBA interface for the file picker to work
With Application.FileDialog(msoFileDialogFilePicker)
' Prevent multiple selections
.AllowMultiSelect = False
' Set the caption of the dialog box
.Title = "Please select a photo"
' Add filters for common image formats
.Filters.Clear
.Filters.Add "JPG Files (JPG)", "*.JPG"
.Filters.Add "JPEG Files (JPEG)", "*.JPEG"
.Filters.Add "PNG Files", "*.PNG"
.Filters.Add "Bitmap Files", "*.BMP"
If .Show = True Then ' File selected
strImagePath = .SelectedItems.item(1)
End If
End With
If strImagePath <> "" Then
' First clear all old photos if desired
If Photo.AttachmentCount > 0 Then
If MsgBox("Clear Previous Photo(s)?", vbQuestion + vbOKCancel, "Remove All Photos?") = vbOK Then
' Clear previous attachments
' (we only want one attachment at a time)
Set rsPhotos = Me.Recordset.Fields("Photo").Value
With rsPhotos
Do While Not .EOF
.Delete
.MoveNext
Loop
.Close
End With
' Clear last displayed photo
Photo.Requery
End If
End If
' Put parent record in edit mode
Set rsParent = CurrentDb.OpenRecordset(Me.RecordSource, dbOpenDynaset)
With rsParent
' Get Cureent Matching Record using Primary Key
.FindFirst "BarCode = " & Me!barcode
.Edit
DoEvents
End With
' Next Add the attachment selected by the user
Set rsPhotos = rsParent.Fields("Photo").Value
With rsPhotos
.AddNew
.Fields("FileData").LoadFromFile strImagePath
If Photo.AttachmentCount > 0 Then
' Rename so it pops up to first file - and keep extension
.Fields("Filename").Value = "00000LatestPic" & Mid$(strImagePath, InStrRev(strImagePath, "."))
End If
.Update
.Close
End With
' Update the parent record
With rsParent
.Update
.Close
End With
Set rsPhotos = Nothing
Set rsParent = Nothing
' Refresh Photo Display
Photo.Requery
End If
End If
End Sub
我正在使用访问数据库作为我们游泳池的会员管理应用程序。我使用报告打印出所有带有照片 ID 的季票。该报告使用一个查询来仅为已拍照的顾客打印通行证。
SELECT tblPassHolders.[PASS HOLDER NAME], tblPassHolders.PHOTO.FileData, tblPassHolders.BARCODE, tblPassHolders.[FAMILY PASS], tblFamilyPass.Expires
FROM tblFamilyPass INNER JOIN tblPassHolders ON tblFamilyPass.ID = tblPassHolders.FamilyID
WHERE (((tblPassHolders.PHOTO.FileData) Is Not Null) AND ((tblFamilyPass.Expires)>Now()) AND ((tblPassHolders.Printed)=False));
这是使用该应用程序的第二年,我遇到的问题是当人们为他们的通行证重新拍照时,然后我的报告打印出 每个人[=25 的通行证=] 照片附件附在他们的记录上。
我对 Access 仍然很业余,我正在努力弄清楚如何编辑我的 report/query 以便报告只打印 one pass for每个赞助人使用附在他们记录上的最新照片。一种解决方案是简单地删除旧照片,这样只有一个附件,但我想弄清楚即使有多个附件,如何让它工作。我一直在玩 DLast()
但我很确定我做错了。
如何进行只显示最近添加的附件的查询?
这是我建议您可以执行的操作的示例,而不是查找最近添加的附件。
如前所述,除非您实施某种命名约定,将最新文件弹出到顶部,否则这是不可能的,因为 Microsoft 按文件名顺序显示附件字段。
这是一个示例,您可以使用按钮来控制附件的插入。 它基于 Stack Overflow example
完成测试后,您可以将 Cancel=True
添加到 Photo_DblClick
事件以完全控制您的照片附件字段
Private Sub cmdAddNewPhoto_Click()
Dim rsPhotos As DAO.Recordset2
Dim rsParent As DAO.Recordset2
Dim strImagePath As String
If MsgBox("Add New Photo?", vbQuestion + vbOKCancel, "Add Photo?") = vbOK Then
' Get New Photo
' Note that you need to add a reference to Microsoft Office ##.0 Object Library
' using Tools | References... from the VBA interface for the file picker to work
With Application.FileDialog(msoFileDialogFilePicker)
' Prevent multiple selections
.AllowMultiSelect = False
' Set the caption of the dialog box
.Title = "Please select a photo"
' Add filters for common image formats
.Filters.Clear
.Filters.Add "JPG Files (JPG)", "*.JPG"
.Filters.Add "JPEG Files (JPEG)", "*.JPEG"
.Filters.Add "PNG Files", "*.PNG"
.Filters.Add "Bitmap Files", "*.BMP"
If .Show = True Then ' File selected
strImagePath = .SelectedItems.item(1)
End If
End With
If strImagePath <> "" Then
' First clear all old photos if desired
If Photo.AttachmentCount > 0 Then
If MsgBox("Clear Previous Photo(s)?", vbQuestion + vbOKCancel, "Remove All Photos?") = vbOK Then
' Clear previous attachments
' (we only want one attachment at a time)
Set rsPhotos = Me.Recordset.Fields("Photo").Value
With rsPhotos
Do While Not .EOF
.Delete
.MoveNext
Loop
.Close
End With
' Clear last displayed photo
Photo.Requery
End If
End If
' Put parent record in edit mode
Set rsParent = CurrentDb.OpenRecordset(Me.RecordSource, dbOpenDynaset)
With rsParent
' Get Cureent Matching Record using Primary Key
.FindFirst "BarCode = " & Me!barcode
.Edit
DoEvents
End With
' Next Add the attachment selected by the user
Set rsPhotos = rsParent.Fields("Photo").Value
With rsPhotos
.AddNew
.Fields("FileData").LoadFromFile strImagePath
If Photo.AttachmentCount > 0 Then
' Rename so it pops up to first file - and keep extension
.Fields("Filename").Value = "00000LatestPic" & Mid$(strImagePath, InStrRev(strImagePath, "."))
End If
.Update
.Close
End With
' Update the parent record
With rsParent
.Update
.Close
End With
Set rsPhotos = Nothing
Set rsParent = Nothing
' Refresh Photo Display
Photo.Requery
End If
End If
End Sub