How to count the sum of same data in access subform - sum

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

Related

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

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.

How to access selected record column value in nested sub-datasheet form

I spend half day to figure out how to access value in nested datasheet form record.
Please, take a look at image below.
I have dblClick event on "SID" column cell. It's field name is "txtSID".
I need to grab that value (in picture "20") and pass it in VBA SQL.
There is some trick with datasheets. Looks like they has no control name or something.
Looks like I found solution. """ & Me![txtSID] & """ does the thing.
Private Sub txtSID_DblClick(Cancel As Integer)
Dim SQL As String
SQL = "INSERT INTO documents (stakeholder_id, document_type_id, status_id) VALUES (""" & Me![txtSID] & """, 1, 1);"
DoCmd.SetWarnings False
DoCmd.RunSQL SQL
DoCmd.SetWarnings True
End Sub

Need top3 records in MS Access

I have to create a text box in MS Access where users are able to see the top 3 records of a particular result set. So even if the query results in 5 records I only want it to display the top 3 records as three textboxes (sometimes the result may also be 1,2 or 0 records).
I took the easy way out and created a new subform which was connected to the parent form using master/child field. The textbox was placed in the details part of the subform and as a recordsource of the subfrom used the following query:
Select top 3 tbl1.column1, tbl1.column2
from tbl1
column1 is the control source for the textbox and column2 is the column I have used for master/child link.
Now the catch is that the query works fine when I use it without top 3. But when I use top 3 the textbox suddenly disappears and the subform is completely blank.
I am not able to identify the cause of the error. My guess is that it has something to do with type of the subform. Not sure.
Is there any other way I can have a text box whose number can vary on the basis of the results?(but limiting the resultset to 3)
Appreciate the help.
Textbox are not meant to hold more than 1 value.
You are trying to assign three results of 2 columns to one textbox(No can do).
Use listbox to populate as you are doing, assigning the query you just wrote in the rowsource of the list(no subforms needed). This way users will see the three records.
You could use a textbox in order to accomplish what you are trying to do. But will require some VBA coding to accomplish this.
Public function CombineValuesForTextBox() as string
Dim rst as dao.recordset
Dim strSQL as string
strSQL = "SELECT TOP 3 tbl1.Column1 as field1, tbl1.Column2 as field2 " & _
"FROM tbl1;"
set rst = currentdb.openrecordset(strsql)
if rst.recordcount = 0 then 'Checks if the recordset has records or not
CombineValuesForTextBox = "No records found"
goto EndCode 'Or replace with what actions to take if no records are found
else
rst.movelast 'Forces the recordset to fully load
rst.movefirst
do while not rst.eof
if CombineValuesForTextBox = "" or CombineValuesForTextBox = empty then
CombineValuesForTextBox = rst![field1] & " - " & rst![Field2]
else
CombineValuesForTextBox = CombineValuesForTextBox & vbcrlf & _
rst![field1] & " - " & rst![Field2]
end if
Loop
end if
rst.close
set rst = nothing
EndCode:
if not rst is nothing then
rst.close
set rst = nothing
end if
end function
Then on your form put in the code (be sure the textbox is unbound...)
me.textbox = CombineValuesForTextBox

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.

DLookup in Access not running until textBox clicked on in Form

I'm setting 12 TextBox ControlSources in my Form from VBA using the following :
...
Me.Oct.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=10 AND Org_Type=[Key]')"
Me.Nov.ControlSource = "=DSum('GBPValue', 'MF YTD Actual Income & Adret', 'Month=11 AND Org_Type=[Key]')"
...
[Key] is the name of a textbox in the form
When the form loads up i get some odd behavior -
all of the summary form text boxes are blank as are all the dlookup text boxes
if i then click on one of the text boxes that has a dlookup control source assigned the summary text boxes for the other columns start to populate with 0's and #Num etc. and the dlookup runs and displays the expected numbers
once i've clicked on all the dlookup fields the summary numbers calc properly.
In the final version of this the query will be re-written after user clicks from the VBA so ... is this a sensible way to get the form to re-query the DB and, if so, how can i make the DLookups run/display automatically so that everything displays immediately on form load?
You are probably looking for Recalc (Me.Recalc). However, I suggest you use a recordset, rather than DlookUp, and the Current event for the form:
Dim rs As DAO.Recordset 'Needs MS DAO 3.x library
Dim db As Database
Dim strSQL As String
Set db = CurrentDb()
'Guessing that key is a form value
'Note that Month is a reserved word
strSQL = "SELECT [Month], Sum(GBPValue) As SumVal " _
& "FROM [MF YTD Actual Income & Adret] " _
& "WHERE Org_Type= " & Me.[Key]
& " GROUP BY [Month]"
Set rs=db.OpenRecordset(strSQL)
'You can probably use a Do While Loop, consider
'naming the controls, eg, Month10
rs.FindFirst "[Month]=10"
Me.Oct = rs!SumVal
'and so on