displaying query results on sub form in MS access - ms-access

I have a Form A (Main form) with combo box taking values from a account master table. Whichever value a user will select from combo box is displayed on the same form in 2 text boxes. Now on pressing a button (On Form A) a query A runs after taking the values in 2 text boxes as inputs.
The results are displayed correctly in a separate tab created automatically in datasheet view.
However I want to display the results on a sub form on the Main form A. I have bound this sub form with the Query A and have placed this sub form on form A.
But still the query results are getting displayed in a separate tab and not on the sub form which seems to do nothing.
Please help.

If the results of the query that you are using as the Source Object for your subform are using the values of the comboboxes as query criteria, you may need to call SubformName.Requery after changing the combobox values.
If instead you are modifying the SQL behind your query rather than using criteria, I have found that you need to issue the following in order for the results to update:
SubformName.SourceObject = Subform.SourceObject
In my experience, when the SQL behind a query that is used as the Source Object for a subform is modified, the data displayed by the subform is not updated following a call to .Requery, but only after the SourceObject property is 'refreshed' using the method shown above.

Related

Is there a way to use buttons in MS Access to fill in text boxes in a report?

Here's the crux of the question, I have 26 compliance queries to run, in a previous question it was suggested that I should filter a single query, or two, on a single report. I like this idea, and have rewritten the query to pull all available data from all the fields, this query works fine. The report will work fine as well, as it does with a model query that I had coded up beforehand. What I would like to do is this:
The end user is being given an interface in access that is locked down, I want them to click a button, and that button will run the query and send to the text box just the field that is called for.
I have tried doing this through VB using the where clause and aliasing the column being called, this did not work at all. I have the report currently pulling the correct data, but not displaying the dates along side it. But it is filtering correctly aside from that.
So what needs to happen is this : Button click : Query runs, and is filtered for "Compliance Issue 1" and puts the dates in "Compliance Issue 1" in the text box on the report.
Right now... I get a list of names, the correct list of names, but an empty column.
I have tried using OpenArgs, but all it did was fill in the date column with "Compliance Issue 1" not the actual data in that column.
Is what I am trying to do even possible in access, and if so does someone have a reference or suggested starting point.
My background : 6 Months of python coding, 3 months of SQL , and some limited access from 20 years ago.
As noted, using the filter of the openreport is without question the way to go (one would not write a whole bunch of different queries - you can send/have any filter for that report - you can EVEN use a sub query in the filter that you send to the report.
As for displaying values in the report that are not from "rows" of data?
There are two approaches that work quite well.
First up, is you have that launcher form. This of course allows the user to select critrea - maybe even some nice combob boxes. These selections take care and you build up the filter in code that you pass to the report.
As for text boxes to be filled out from that form and inclluded in the report?
If they are static values from the report (say filter options, or even just a notes box that you could type in some text? To display such values in textboxes on the report?
You can directly set the text box data source (in the designer) to the report propter form like this:
=(forms!MyPromptForm!notes)
So, any value you shove into text boxes on the report prompt form can thus be displayed in any text box on the report with the above type of expression. And it does not even take code to achieve this goal. So, you could say with above enter some notes into that text box, and thus on the report, whatever you typed into that text box will now show up in the report. You just drop in a text box onto the report, and set the data source of the text box to the above expression that references the form with the values we want from that form.
The next approach, and I often use this in the case that some value/expression/calculation has to occur for each row. In this case, you can use the reports on-detail format event. This allows you to run code for EACH row of data.
You are free to run ANY code in that event - and that includes after running such code to set a text box in the reports detail section.
So, say the query only had the Hotel ID (PK). This is a lame example, but you could then write this code in the on-format event of the reports detail section.
dim strSQL as string
dim rst as DAO.RecordSet
strSQL = "SELECT HotelName from tblHotels where ID = " & me.HotelID
set rst = CurrentDb.OpenRecordSet(strSQL)
me.HotelName = rst!HotelName
rst.Close
So in above, we assume that a row text box is called HotelID, and then in code we build a whole sql query from scratch, pulled the row data from a table, and then SHOVE/SET the value of the un-bound text box called hotelName.
As noted, the above is a simple example, but we are free to run any code we want, pull any data we want, and set the value of ANY text box for the given detail section ONE row of values.
So, above shows two approaches. The first approach is code free - and you can put forms! expression directly into the report, and the values from that report prompt form will thus show up directly in the report. However, if you need VBA code to run for each row, pull values, walk the dog, and THEN set a text box on that one details row of data, then you are as above shows free to write procedural code in the report that fires + runs for each row of data - and that means you can quite much do anything you want in terms of running code. I mean, even that on detail format event COULD pull values from your report prompt form, but as the 1st example shows, you can shove in forms! expression directly into a text box - and those forms! expressions can be values from a existing form that is open before the report is launched.

Access 365 - Error When Populating Unbound Text Field from Combo Box

Sorry in advance for the long description, but it seemed like the prudent course of action.
I am having a problem getting a combo box to correctly populate a text field in a subform.
Based on surfing the web and some helpful guidance on adding VBA to forms earlier this week here is what I tried.
I have created a table tblPgo showing several probabilities of a project proceeding and a corresponding descriptions.
I have a second table tblDetails containing information about the items we are selling.
These two tables interact in a subform tblDetals subform.
I added a combo box cmbPgoValue to the subform. The combo box gets its data from tblPgo
Control Source is Pgo
Row Source is SELECT [tblPgo].[PgoID], [tblPgo].[Pgo], [tblPgo].[PgoDescription] FROM tblPgo ORDER BY [Pgo];
The Row Source syntax was created by the combo box wizard.
In the subform, I created an unbound text box called Pgo Description to receive the PgoDecription text corresponding to the selected Pgo value from the combo box.
cmbPgoValue has the following After Update Event code
Private Sub cmbPgoValue_AfterUpdate()
Me.PgoDescription = Me.cmbPgoValue.Column(2)
End Sub
When I use the combo box to select a Pgo value, the correct Pgo Description populates the current record and the record below it. When I try completing the incorrectly populated record, I get an error about duplicating indexes, etc.
I've tried bounding the text box, but it does not seem to help.
Thanks in advance.
That is to be expected with UNBOUND textbox. There is only the one Description textbox so all records will show the same info. Should not be duplicating this info into tblDetails. Instead of VBA, just have expression in textbox ControlSource:
=cmbPgoValue.Column(2)

Populating listbox from another listbox selection

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Access 2003 - Query can't get combo box value off of subform

I am trying to use a combo box's value from a subform to use as the criteria for a query. It I use the form as a stand alone form, with this in the criteria field, it works fine:
Forms!FormStats!ControlName
I then try to run it as a subform on a main form with this code, and it doesn't work:
Forms!MasterForm!FormStats.Form!ControlName
I've been trying all day to figure this out with no luck. What am I missing?

Access 2010 - Report asks to enter parameter value

I have a report with a subreport. Report is unbound (it is used as a template for letter and contains plain text only). Values in the subreport are based on values of two combo boxes located in a form.
So the logic is as follows - when the form (frmForReport) is loaded the user selects values in combo boxes intended to filter the database (cboSelectCompany and cboSelectPU), then clicks OK button which opens my report with its subreport. Subreport is populated from a query where two of fields have those combo boxes as criteria. Everything works fine (I select what I need in combo boxes, report opens in Report View with the subreport showing all filtered records without any warnings or requests) until I want to switch to Print Preview or Create PDF. Every time when I do so I see pop-up window Enter Parameter Value with a reference to one of criteria used in the subreport. When I close this window I can proceed further, save it as pdf or switch to Report View but the subreport appears blank, literary disappears.
This is for Access 2010. Below is the query used as a record source for the subreport:
SELECT qryForLetter.CrossingID, qryForLetter.DispID, qryForLetter.AgreType, qryForLetter.Legal
FROM qryForLetter
WHERE (((qryForLetter.Company)=[Forms]![frmForReport]![cboSelectCompany]) AND ((qryForLetter.PU)=[Forms]![frmForReport]![cboSelectPU]));
What do I miss? How do I get rid of this pop-up window? Any help is greatly appreciated.
You could try replacing parameters [Forms]![frmForReport]![cboSelectCompany]) and [Forms]![frmForReport]![cboSelectPU] with functions.
Create global variables in the header of a module:
Public Company As ...
Public PU As ...
... replacing the ellipses ... with the correct datatype.
In your Combo Boxes' AfterUpdate events, assign the value associated with the Combo to the appropriate global variable, e.g.:
Company = cboSelectCompany
and
PU = cboSelectPU
and create functions (again replacing the ellipses ... with the correct datatype):
Public Function fnCompany() As ...
fnCompany = Company
End Function
Public Function fnPU() As ...
fnPU= PU
End Function
Then change the data source to:
SELECT qryForLetter.CrossingID, qryForLetter.DispID, qryForLetter.AgreType, qryForLetter.Legal
FROM qryForLetter
WHERE (((qryForLetter.Company)=fnCompany()) AND ((qryForLetter.PU)=fnPU()));
This has the effect of removing the dependence of the report from the form fields, which appears to be the issue.