Count row in ms access report that have same data in 2 column

时光毁灭记忆、已成空白 提交于 2019-12-11 17:45:48

问题


  1. form before that user need to select and when search button is clicked, it will show a report based on user selection

  2. this is the report after the search button is click

Hi expert. I have a problem in looking for rows in ms access report that have duplicate data across columns family and name. So if in the first row column family = a and name = b, and in another row family = a and name = b, then we have a duplicate row regardless of other columns. I want it to count from the report not from the table or query. This is because the report will show based on user selection on combo box and list box from other form. and when the search button was clicked, then it will generate the report.

Therefore, I would like to have a button "Summary" in report where its can show result like below (based on report form):

the result

and so on ....

I hope i can get a positive feedback from you guys. Thanks

Below are the code that i used to generate the report :

Code for button report


回答1:


one way to do this without passing more than 1 parameter is to place the summary in a sub report and reveal that sub report with the push of a button. Unfortunately sub reports in footers are buggy in access so you have to filter the sub report manually. we start with a similar simple normalized database: Then I added a simple form with a multi-select listbox of families and a button to open a filtered report.

Private Sub cmdSearch_Click()
'Build Filter for report
Dim strFilter As String
Dim firstselectedfamily As Boolean
firstselectedfamily = True
For i = 0 To lstFamilys.ListCount - 1
If lstFamilys.Selected(i) = True Then
If firstselectedfamily = True Then
strFilter = "FamilyFK = " & lstFamilys.Column(0, i)
firstselectedfamily = False
Else
strFilter = strFilter & " OR FamilyFK = " & lstFamilys.Column(0, i)
End If
End If
Next i
'open report with filter
DoCmd.OpenReport "ExampleReport", acViewReport, "", strFilter, acNormal
End Sub

here is the ExampleReport with a button to show a summary of duplicates:

The button reveals the hidden sub report based on a query that finds the duplicates:

The duplicates query is made by grouping based on family and test where both the count of familyID and TestID is at least 1: Normally the summary report would be linked to the main report by a master child relationship, but the summary looks natural in the main reports footer where sub reports happen to be bugged and do not filter correctly. To get around the bug we provide code to filter the summary report:

Private Sub cmdSummary_Click()
'filter summary form by using the main reports filter
Me.subfrmSummary.Report.Filter = Me.Filter
Me.subfrmSummary.Report.FilterOn = True
Me.subfrmSummary.Requery
'show/hide summary form
If Me.subfrmSummary.Report.Visible = False Then
Me.subfrmSummary.Report.Visible = True
Else
Me.subfrmSummary.Report.Visible = False
End If
End Sub

Again, to get around the bug do not link the sub report to the main report with a master/child relationship. Instead set the filter with code.




回答2:


You can build an aggregate query based on your report query, and build a report based on this query.

SELECT Family, whichTest, Count(*) as ProductCount FROM Report_Query_Name GROUP BY Family, whichTest

This takes in to consideration that you have the report query saved as a separate query outside of your report.



来源:https://stackoverflow.com/questions/54761043/count-row-in-ms-access-report-that-have-same-data-in-2-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!