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

form before that user need to select and when search button is clicked, it will show a report based on user selection
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

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.

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.

Related

Putting results of array into a form control

I have created an array in VBA in my Access application, and when I run it I get the intended results in the immediate window. What I am trying to do now is to take that array and put it into a form control (possibly a subform dataset?). I do not know how to this though on a multidimensional array. Below is the code that creates my array (it's dynamic, it can contain from zero to 10 results depending on how many records are in the DB.
' We need to get all of the participants for this engagement (Name, Email, Division and Role)
strSQL4 = "SELECT tblPerson.Person_FName, tblPerson.Person_LName, tblPerson.Email, tblORD_Division.ORD_Div, tblEngParRole.Role FROM tblORD_Division INNER JOIN ((tblEngagements INNER JOIN tblEngParRole ON tblEngagements.ENG_ID = tblEngParRole.Eng_ID) INNER JOIN tblPerson ON tblEngParRole.Person_ID = tblPerson.Person_ID) ON tblORD_Division.ORD_DIv_ID = tblPerson.ORD_Div_ID WHERE tblEngagements.Eng_ID = " & Me.Eng_ID
Set rs4 = CurrentDb.OpenRecordset(strSQL4)
rs4.MoveLast
rs4.MoveFirst
'Let's retrieve ALL Rows in the rs4 Recordset
varParticipants = rs4.GetRows(rs4.RecordCount)
Debug.Print "******************************************" 'Column Format only
Debug.Print "Last Name", "First Name", "Email", , "ORD_Div", "Role"
Debug.Print "---------------------------------------------------------------------------------------------"
For intRowNum = 0 To UBound(varParticipants, 2) 'Loop thru each Row
For intColNum = 0 To UBound(varParticipants, 1) 'Loop thru each Column
'To Print in Table Format, no numbered Fields or Rows
Debug.Print varParticipants(intColNum, intRowNum),
Next
Debug.Print vbCrLf
Debug.Print "******************************************" 'Column Format only
Next
rs4.Close
Set rs4 = Nothing
Thanks
Access doesn't provide a convenient way to display an array on a form. Skip the array and use a form/subform approach to display your data.
Build a query similar to your current query, but with these two changes:
Include tblEngagements.Eng_ID in the list of SELECT columns.
Discard the WHERE clause so the query will return all rows regardless of their Eng_ID values.
Build a new form based on that query.
Add the new form as a subform to your original form. On the subform control's property sheet, set the Link Master Fields and Link Child Fields properties to Eng_ID
Then, in operation, the subform should display only those rows whose Eng_ID values match the Eng_ID of the current row in the parent form.

MS Access - run report from a form that has 2 buttons that use the same report but have different results

We would like to run a form that will have 2 buttons that use the same report but have different results:
Button 1 - open report for current record (which we have working) using
Private Sub Command25_Click()
DoCmd.OpenReport "Bale Spike Certificate", acViewPreview, , "[no] = " & Me.[no]
End Sub
Button 2 - open (Bale Spike Certificate) report (same report that button 1 points to) but when it opens it requests the user to enter the serial number [no] which will show the report of the entered serial number.
Can get the report to work off a query by using a criteria on the [no] field, but when I change the report to use the query as the record source, button 1 (current record print) stops working and the other way round, if I use DoCmd for the current record.
Is there a way of achieving this? Or do I create 2 reports for each button?
You could use an InputBox:
Private Sub Button2_Click()
Dim SerialNo As Long
SerialNo = Val(InputBox("Enter Serial No.:", "Bale Spike Certificate"))
If SerialNo > 0 Then
DoCmd.OpenReport "Bale Spike Certificate", acViewPreview, , "[no] = " & SerialNo & ""
End If
End Sub

How to count the sum of same data in access subform

I have two access subform.
First Subform (Pivot1234Subform)
1) First is (Pivot1234 subform). Its take pivot1234 table format. Then,this subform will show the data after user selection on listbox and dropdown text box. below are the code that generate this subform:
Generate PIvot1234 subform
Second subform (CountDuplicateSubform)
CountDuplicateSubform
this subform is follow query wizard duplicate data of pivot1234 table.
the purpose of this subform is to count the sum of data that containt same data in column FAMILY and WhichTest from pivot1234_subform after the summary in pivot1234 report is clicked.
example
if first row : family = a , whichtest =b
then second row : family =a , whichtest = b
it will appear in column third (count) "2"
example of the result
below are the code that i already try in Form_Load event:
{Private Sub Form_Load()
Dim strSQL As String
strSQL = "SELECT First(Pivot123_subform.Family) AS
[CountDuplicate_subform.Family], First(Pivot1234_subform.WhichTest) AS
[CountDuplicate_subform.WhichTest], Count(Pivot1234_subform.Family) AS
[CountDuplicate_subform.Count]" & _
"FROM Pivot1234_subform" & _
"GROUP BY Pivot1234_subform.Family, Pivot1234_subform.WhichTest" & _
"HAVING (((Count(Pivot1234_subform.Family))>1) AND
((Count(Pivot1234_subform.WhichTest))>1));"
Forms!CountDuplicate_subform.RecordSource = strSQL
Forms!CountDuplicate_subform.Form.Requery
End Sub

VBA - Based on User Input - look up value in table

I have created a form that asks for two user inputs, site location and sku. Site location is a drop down and SKU is a text box. Below it there is a textbox which I want to populate based on user input after they hit the "whats my price?" button.user form
I have a matrix of prices with the SKU in column B and the sites across the top in row 1 with their respective prices in the matrix(columns D-H). I have attached a sample of the table. Please note that the "SKU" and "Site" titles will not be in my actual matrix.
pricing table
I need assistance coding the "What's my price?" button in the user form.
I feel as though I would need an if statement using some sort of look up but i'm a little lost as to how to start the code.
Here is what you have to do:
Read the value from the site field;
Read the value from the SKU field;
Display the matching in the Your Price is field, using the following formula:
WorksheetFunction.Index(Range,site field, sku field)
More about WorksheetFunction.Index here:
https://msdn.microsoft.com/en-us/library/office/ff197581.aspx
You need to use Application.Match on the row and the column independently, then get the corresponding cell. Try the following code, but replace the control's names (txtSku, cmbSite) and the sheet's code name (mySheet) with yours.
Sub WhatsMyPrice_Click()
Dim rowNum, colNum
rowNum = Application.Match(txtSku.Value, MySheet.Range("B:B"), 0)
If IsError(rowNum) Then MsgBox "SKU not found": Exit Sub
colNum = Application.Match(cmbSite.Value, MySheet.Rows(2), 0)
If IsError(rowNum) Then MsgBox "Site not found": Exit Sub
txtPrice.Value = MySheet.Cells(rowNum, colNum).Value2
End Sub

Access textbox with multiple values to query

I have an Access database, where I have a form with a textbox (named c1) and a button. When I click the button it opens a datasheet form with information filtered by the textbox value.
The button vba looks like this:
c1.Value = Replace(c1.Value, ",", " Or ")
DoCmd.OpenForm ("dsForm")
The query behind the datasheet looks something like this in design view:
Field: Name1 | Name2
Criteria: | Like [Forms]![Menu]![c1].[value]
This is so I could later export the results of this query to excel.
So my issue is that I want to enter values into the textbox and separate them with a comma, which would be later turner into an Or by vba. Why I'm doing this with 1 textbox not multiple, is because I could have many values that I want to search by.
Right now it works if I enter one value into the textbox, but when I enter 2 values it's not working. I'm pretty sure that the query is taking the whole statement as a string for example if I enter 110,220 it's supposed to be Like "110" or "220", but on the query it would be Like "110 or 220".
I've tried by setting the field to be either a string or a number as well. How would I manipulate the criteria on a query from vba?
I recommend writing a SQL string with the IN statement instead of the OR, and using the OpenArgs event to pass data from the main form over to the datasheet form.
Main Form Button Code
Dim sql as String
sql = "Select * From [table name] Where Name2 IN (" & c1 & ")"
DoCmd.OpenForm "dsForm", acFormDS, , , , , sql
Datasheet form (dsForm) Code -- Use the Form_Load event.
Private Sub Form_Load()
Me.RecordSource = Me.OpenArgs
End Sub
The IN statement allows you to use commas. The OpenArgs event allows you to pass values from one form over to another.
Actually my first method was terrible, read the values into an array like this:
Sub y()
a = "a,b,c,d"
'Split into 1d Array
b = Split(a, ",", , vbTextCompare)
For c = 0 To UBound(b)
Debug.Print b(c)
Next c
End Sub
You can loop through the array as in the debug.print loop and use each value separately.