Group records in a report - ms-access

This script filters records based on user input (Startdate and enddate) from a prior screen. It works properly, but I want the outputted report to group the data by another field, [SN]. How would I add that parameter?
Me.Filter = "[Date] between #" & Format(Me!startdate.Value, "yyyy\/mm\/dd")
& "# and #" & Format(Me!enddate.Value, "yyyy\/mm\/dd") & "# "
strFilter = Me.Filter
Me.FilterOn = True
DoCmd.OpenReport "Monthly Ops", acViewReport, , Me.Filter

Don't think you can dynamically 'group by' in Access reports. Assuming you have a limited number of fields that you might want to group by, I would create that number of different reports, each with one of the different grouping options, and then use your other parameter to determine which report to open.

The Access Report Wizard give the option of grouping on one or more fields. I was able to group on SN and sort by date, and I think I will filter using a query prior to sending records to the report.

Related

Access VBA Keyword Search Function on multiple fields

I currently have a simple Access database with forms for the users to fill out based off a queried table.
My goal is to use a search box that can filter results based off a keyword multiple times. My existing code works great for a single search on 1 field. I want to be able to drill down off the first search by searching off another field. After I select my field from combo box and search keyword, my results are displayed. Once I pick another field from the same box and search, the results do not include my 1st filter.
On the form, I already have a combo box with a list of all the fields to choose from. Then next to that is a text box for the user to search off the chosen field list. I have correct VBA code to search off a single field, but I'd like to drill down from there. Basically, I want the ability to search a keyword on a selected field, and then be able to filter those results further by using the same search box again.
Example: On form, select "borrower" from drop down list and type "Smith" in search box, click search button. THEN I'd like to choose another field such as "Issue Category" from the same drop down list and type "late payment", then click search button. Thus, giving me all records containing the borrower Smith where issues exist of late payments.
I've been spending days on this and finally broke down to come here. I need to know what code I'm needing to add that would accomplish my goal of multiple searches without filter resetting. I am hoping you can help. Here is my code (Text35 is the text box and searchlist is the combobox list of field names):
Private Sub Search_Click()
Dim strSearchValue As String
strSearchValue = Me.Text35.Value
Select Case Me.searchlist.Value
Case "Date"
Me.Filter = "[Date] = #" & strSearchValue & "# "
Case "Account number"
Me.Filter = "[Account number] = #' & strSearchValue & '# "
Case "Borrower"
Me.Filter = "[Borrower] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
Case "Issue Category"
Me.Filter = "[Issue Category] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
End Select
Me.FilterOn = True
End Sub
I think you would use the OR keyword instead of &

Access report prints too many pages with filter applied

I have a button in an Access 2007 Form with the on click VBA code as follows:
Private Sub Command53_Click()
DoCmd.OpenReport "BolLSW", acViewNormal, , "[bolnum] = '" & [bolnum] & "'"
End Sub
Essentially this will open a report (based on which number you enter for bolnum when pressing the button) which has the same information as the form and then print it. However when the report prints it print all the records instead of just the one entered.
Is there a way to specify to only print the desired record?
To clarify: Bolnum is a unique field within the form that auto increments. Clicking the "Generate BoL" prompts a dialog box for you to enter the Bolnum to print. Upon entering 2, both record 1 and 2 will print.
I'd try giving [bolnum] (between the 2 &, i.e. the variable ) a different name. I guess Access is taking the value of the current row - so the where-clause is always true.
I've solved this by removing some of the quotes from
DoCmd.OpenReport "BolLSW", acViewNormal, , "[bolnum] = '" & [bolnum] & "'"
specifically:
removing
>'<" & [bolnum] >& "'"<
resulting in:
DoCmd.OpenReport "BolLSW", acViewNormal, 1, "[bolnum] = " & [bolnum]

pass parameters to record source with query Access2007

I am relatively new to access and I have been tasked to improve the navigation of the app.
I have a form which is used to open the report.
The record source of the report is as follows,
SELECT
Projects.Project_Number,
Projects.Customer,
Projects.End_User,
Projects.Engineering_Company,
[Merged Ship and Delivery Dates].Sched_Next_Delivery_Date,
[Merged Ship and Delivery Dates].Sched_Next_Ship_Date,
Projects.QC_Pack_Req,
Projects.Target_QC_Pack_Date,
Projects.Invoice_QC_Pack
FROM
Projects LEFT JOIN [Merged Ship and Delivery Dates]
ON Projects.Project_Number = [Merged Ship and Delivery Dates].Project_Number
WHERE
(((Projects.Project_Number = [Project Number] )))
ORDER BY Projects.Project_Number;
I am trying to get the report to open up without prompting me every time. There are instances where I need to refresh the report or open it from other forms.
I have tried to use
DoCmd.OpenReport "Project Control Sheet", _
acViewReport, , _
"[Project_Number]=" & Me.ProjectNumber, , _
"[Project_Number]=" & Me.ProjectNumber
It is still unable to pass the parameters to the record source. Is there anyway for me to pass the parameters to the recordsource(query)?
I have tried to use
Forms![formName]ProjectNumber
in the where statement but this only works for a single form and I have other forms which opens up this report.
The purpose of refreshing the form is to allow users to view the changes made to the report after they have updated it.
If you remove the WHERE clause from the report's Record Source query, you can later filter the rows it returns with the WhereCondition option of the DoCmd.OpenReport method.
Dim strWhereCondition As String
strWhereCondition = "[Project_Number]=" & Me.ProjectNumber.Value
Debug.Print strWhereCondition ' <- view this in Immediate window; Ctrl+g will take you there
'DoCmd.OpenReport "Project Control Sheet", acViewReport, , strWhereCondition
DoCmd.OpenReport ReportName:="Project Control Sheet", _
View:=acViewReport, WhereCondition:=strWhereCondition
If you want a different approach, you could use a TempVar, since your Access version is 2007.
TempVars.Add "ProjectNumber", Me.ProjectNumber.Value
And change the Record Source query to use the TempVar ...
WHERE Projects.Project_Number = [TempVars]![ProjectNumber]

Access Two Table SubReport - String Where

I have a quick question about reports. I have a report that has two unbound subreports (the data is from two different databases and do not share any information. I usually opened these reports individually by using a simple UI to select different options and generate a string where and open the report ie,
DoCmd.OpenReport str_rptname, acViewReport, , strWhere
strWhere is made by the UI
Now that I have combined them into one report the string where does not work. Is it possible to pass a where string to a subform? If so how would I go about doing so? Is there another/better way to do this?
Thank you!
as far as I can see you pinpointed the problem correctly as you wrote in your comment:
I believe it is because the string where opens the main report and
sets the filter there, but the sub reports are not bound so open
unfiltered
Because the subreport are unbound you need to give them a complete recordset and not just a WHERE clause. Like this:
dim whereClause As String
dim strRecordset as String
'***Build your where clause
strRecordset = _
"SELECT yourColumns " & _
"FROM yourTables " & _
"WHERE " & whereClause
Me.subreport.recordset = strRecordset
Me.subreport.Requery
Requery might be optional.

Filtering a report query

Still a beginning access programmer here. Trying to get the report to work. Here is what I am doing
I first created a report using the Report Wizard using the following query as input
SELECT EmployeeId, Project, StartDate
FROM Tasks;
I have a form wherein I select the employee-id. I want to filter the report based on the employee id selected. Here is what I have for invoking the report
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = " & strempid
qryEmpReport is the name of the query that holds the report query that I mentioned above. The strempid holds the value that was selected in the form. However when I get around to execute this, it prompts me to enter the employee id again. Any ideas as to why I am getting this? I have validated to make sure that the strempid does contain the value selected earlier.
I'll guess Tasks.EmployeeId is text datatype. If my guess is correct, add quotes around the value you supply for EmployeeId:
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", "EmployeeId = '" & strempid & "'"
Based on our trouble-shooting exercise in the comments, I think you should give yourself an opportunity to examine the actual string value you're giving to OpenReport for its WhereCondition argument. (It's better to view the actual string instead of trying to imagine what it looks like.)
Dim strWhereCondition As String
strWhereCondition = "EmployeeId = '" & strempid & "'"
Debug.Print "strWhereCondition ->" & strWhereCondition & "<-"
DoCmd.OpenReport "rptEmpWork", acViewPreview, "qryEmpReport", strWhereCondition
View the output from Debug.Print in the Immediate window; Ctrl+g will take you there.