Access Report, get value from parent report - ms-access

I have a report. Within that report is a sub report. And, there's a sub report within the first sub report. Anyway, each report has a stored query as its data source. The structure looks like this:
Main Report
Sub Report 1
Sub Report 2
I would like the stored query in Sub Report 2 to reference a field in Sub Report1. But, every time I run the report, I get a message box asking for the value. Here is the bit in my query that references the field:
[Reports]![MainReport]![SubReport1]!Report![Text70].value
Anyone know the correct syntax?

Not sure why, but leaving off value seems to have done it.
So, just something like: [Reports]![MainReport]![SubReport1]!Report![Text70] works.

Related

MS Access 2016: Set Report's RecordSource to get data from a Subform

Is there some sort of work-around that could make this possible? Or could anyone offer just some general advice on my situation below? I tried to set the record source through VBA but had no luck.
My situation:
I have a main form with a subform contained within, and I want the subform to be purely for data entry (Data Entry = Yes). Upon clicking a button, the user is sent from the main form to a report (print preview only). I want the source for this report to be the subform the users are entering data into, but I don't have this option from the report's property sheet itself (no forms), and I also was unable to set it through VBA. Here's my code, it's one line so I highly doubt it's the problem.
Private Sub Report_Load()
Reports![P18003 SDR Report].RecordSource = "P18003 SDR Subform"
End Sub
Previously, to work-around this I had a parameter query that waited for the user to enter data into an unbound textbox from the main form, and an after update trigger on that textbox would load any data relevant to that parameter (my employer and I miscommunicated the requirements).
It turns out though that I don't need to load any older data, and I run into problems with my current parameter query in the event that the user does input a parameter which loads old data - that old data is transferred to the report in addition to the new data they've just entered. This is the main problem with the method I am currently using, and it trashes almost all functionality with this form and report. The events that a user would need to enter a parameter which queries older data are few and far between, but it's not a functional product unless I can get the subform to be connected to the report directly. There's likely something obvious I'm missing here (or at least, I hope there is).
Thanks in advance for taking the time to read this and for any help you may offer.
you can either send the recordsource to the report itself in the OpenArgs by the open report event, after the click event in the subform
Private Sub btnSubform_Click()
Dim strSQL as String
strSQL = "SELECT * FROM SubformQuery WHERE ID = " & CurrentSubfromRecordID
Docmd.OpenReport, acViewReport, , , , strSQL
End Sub
and then in the Report.
Private Sub Report_Open(Cancel As Integer)
Me.recordsource = Me.OpenArgs
End Sub
Or you can make a paramter Query and set this Query criteria as record source for the report. So the parameter in the query is pointing to the current selected row in the subform. Like this:
--- At the Criteria in the Query designer:
--- RecordSource for the Report:
Forms![FormMain]![YourSubform]![ID]
Now every time the reports obens he gets the ID from the current selected record of your subform.

VBA run Access Report with Parameter based on query

I want to automate some of our Access Reports with a VBA script. But when I want to run them with DoCmd.OpenReport, I have the problem that every Report has a Popup for some Parameters coming from the underlying Query.
This is how the Parameter looks like in the Query:
PARAMETERS [Time] Text ( 255 );
I googled a lot and tried with:
DoCmd.OpenReport "B_My_Report", acViewPreview, , "[Time]= 423"
But this didn't work; the popup still comes and when I enter nothing, the query will fail because Time parameter is empty.
Is there a way I can call the Report with the Parameter value. I read a lot of Suggestion to remove the Parameter completely and use the where condition in OpenReport. But I can't change these Queries because they aren't made and maintained by me. I only have to run them sometimes. So I would love to have a solution without touching the Report or the Query.
If your Access version is >= 2010, consider the DoCmd.SetParameter Method.
This query is the Record Source for my report:
PARAMETERS which_id Long;
SELECT rmy.id, rmy.reportDate, rmy.gainOrLoss
FROM record_matYields AS rmy
WHERE rmy.id=[which_id] OR [which_id] Is Null;
So then I can assign a value for the parameter and open the report displaying only the matching record (id is the primary key):
DoCmd.SetParameter "which_id", 4
DoCmd.OpenReport "rptFoo",acViewReport
Or, because of the OR [which_id] Is Null condition in the query's WHERE clause, I can assign Null to the parameter before opening the report if I want all records included regardless of their id values:
DoCmd.SetParameter "which_id", Null
You can't. If you would open the query in VBA, you could supply the parameter. But since the report is the only one who calls the query, the query will ask for its parameter.
If you can't change the query, you'll have to live with it.
You can refer the parameter value from a form that will open this report using the syntax Forms![form name]![control name], so the query needs to be changed something like
Select * from [table name] where time_id= Forms![form name]![control name]
this will make sure the the query gets parameter from the form's control and it won't prompt parameter.
But it only works if you open that query when this form is kept open/loaded...otherwise it will prompt like parameter again.
The Docmd.setparameter in Access 2010 seems to be a very good suggestion where it keeps flexibility and good programming.

Can I use criteria from any current form in a single query in Access 2003

I have a report (ReportX) that I wish to open from two different forms (FormA and FormB) in my database. I wish to do this because FormA and FormB address different aspects of my data, even if they ultimately get to the same output. ReportX is based on data from QueryX.
The problem I have is that QueryX would ideally filter the data based on the current RecordID in the current form. But I don't know how to accomplish this. I'd like to design QueryX so that the criteria for RecordID is essentially CurrentForm!RecordID, but research suggests that I cannot do this. Must I make separate but otherwise identical queries and reports for each form? Or is there a way to use VBA to define the query criteria when I click on the OpenReportX command button?
I already tried using the WHERE condition in the OpenReport command:
DoCmd.OpenReport "ReportX", acViewPreview, ,"RecordID = " & RecordID
but that did not display the results I wished. I need the report header to display/print for each RecordID and the page count in the page footer to reflect only the current/total pages of the RecordID in question. (In other words, if record 1 is one page, record 2 is two pages and record 3 is three pages, then ReportX, when displaying the first page of record 2, should say "Page 1 of 2" and not "Page 2 of 6.") So being able to display and print a single record properly using record filters would also solve my problem.
Which is the least cumbersome/most possible solution?
You should be able to accomplish this using the WHERE condition argument when you open a report:
DoCmd.OpenReport "rptName", acViewPreview, ,"RecordID = " & Me!RecordID
In the case where a I need more control over a Report's recordsource than what the Where condition argument can do for me, I will set the Reports RecordSource to be blank (after designing the report). Next I write code create the correct SQL statement in the Open Report button's Click event, followed by code to open the report and pass in the SQL as an opening argument for the report.
Private Sub cmdOpenReport_Click()
Dim sSQL as string
sSQL = "SELECT * FROM tblWhatever WHERE RecordID = " & Me!RecordID
DoCmd.OpenReport "rptReportName", acViewPreview, , , ,sSQL
End Sub
Then in the report's Open event I write code to set the recordsource:
Private Sub Report_Open(Cancel As Integer)
If IsNull(Me.OpenArgs) = False Then
Me.RecordSource = Me.OpenArgs
End If
End Sub
If neither of those accomplish what you want then you have an issue of a different sort. It's a little difficult for me to understand why you need All Records to show up in the header but only one record in the detail area. And how you expect to accomplish this. You might be best off trying to write a query first that gives you the exact results that your looking for so you know that it can be done.
As a side note, I actually use very few saved queries in my designs. It's not that there's anything wrong with using them, as the option is there for your convenience. I frequently use raw SQL on both forms and reports and set the RecordSource to the SQL on the Form or Reports Open or Load events.
Yes you can point to form data items in a query, and subsequently use that query in a report. The forms need to be open before the report runs. As far as having a header for each record, that is controlled in the settings of the report and how it displays the data.
In the Field, or Critera you can use the format:
[Forms]![frm_Process_Candidates]![QuestionTemplate]
Where frm_Process_Candidates would be the name assigned to your form, and QuestionTemplate is either the name of a control on your form, or a field from the data source of your form.
If you have a sub-form, there will be another [Form] call in the middle there.
[Forms]![frm_Dropdown_Admin]![frm_Dropdown_Admin_Detail].[Form]![text22]
Access should figure it out from there.

Difficulty in avoiding empty space when a sub report returns no data to main report

I am working on a a multi level report, meaning I have two sub reports embedded inside a master report. I have palced one sub report inside a group so that it will run multiple times, once for each group.
Every thing is working fine: sub reports are generated as should be. The actual problem comes when sub report returns nothing: because the sub report is run multiple times, whenever the sub report is empty, the main report is filled with gaps.
Is there any way to avoid that? Can I check whether the dataset is empty, or is there some other solution for this?
If you have your sub report's report items like tablix etc embedded inside Rectangle. You would see this behavior.
There is a property in sub-report element Remove Line When Blank. Make it true.

MS Access How can I show the query name in a report

If I make a report in MS Access then it is possible to let the program automatically fill in the name of the report, the actual time, the name of the report etc with the help of the code [name] Date() Time()
My question is if it is also possible that the program shows the query name (which is used for the report) on the report.
I have some reports on which I frequently change the query and it would be nice if the report automatically shows the name from the used query.
This method worked for me with Access 2003.
I added an unbound text box, txtRecordSource, to the report footer. Then used this for the footer's format event.
Private Sub ReportFooter_Format(Cancel As Integer, FormatCount As Integer)
Me.txtRecordSource = Me.RecordSource
End Sub