Return subform values based on muliple criteria in Access - vba

I need to select one or more values from the same field in subform 1 and display the applicable values in subform 2.
I have done this kind of thing by selecting criteria from multiple fields but not with multiple line-item values from the same field. My first thought was to build a dynamic SQL string to populate subform 2 but just wondering if there is an easier way to do this.
At the moment if I select a line-item in subform 1 (flight itinerary), subform 2 will only show the applicable flight segments for that itinerary. If a customer has more that one flight itinerary for the same trip in subform 1 I need to show all flight segments for the entire trip in subform 2.

Thanks to Lee Mac's suggestion to utilize the IN() function/operator, I was able to feed the IN() function with a subquery string to return the data I need without having to loop through a recordset. Just had to ensure that the subquery only returned values for a single field (in this case the ID field) or it would error out.
I created a function to fill in the Segment numbers in order.

Related

sql query expression

The table retrieves data from the table "Group" with 2 columns First key "GroupNumber" and GroupName"I am trying to create a sql query in Microsoft Access from a table with two columns that will works as below:
Based on the user's selection from the first column the query has to return the value of the second column, and based on the user's selection from the second column the query has to return the value of the first.
Any idea how is possible to express the user's selection in sql? Ty
Some more data would be useful. AT this moment, it is hard to understand what it is exactly you are trying to do.
However, you mention two different selecting actions that need to be done by the user, so two SELECT statements would work.
To return the value of the second column based on user selection of the first: SELECT second_column_value FROM table WHERE first_column_value = value_selected_by_user
To return the value of the first column based on user selection of the second:SELECT first_column_value FROM table WHERE second_column_value = value_selected_by_user
or you can use drop-down lists, if the situation allows
Try providing more data to get more useful answers.

Access query based on filtered subform only returns first subform record

I have a form with unbound comboboxes that are used to filter a subform based on the combobox value selected by the user. The subform is based on a query that pulls data from the source table. The subform can be filtered on fields in the query. The subform filters the way I want it to when the comboboxes are used. The issue is I want to have a command button that opens a report that displays the currently displayed results of the filtered subform.
I have a report that uses a separate query as the record source. The query criteria for the record ID is set to the record ID field in the subform. The problem I am having is that the query that serves as the record source for the report will only return the first value from the subform.
The SQL statement is below. I have tried removing the WHERE clause and using different join types. If I remove the WHERE clause the query will return all of the records from the source tables regardless of join type. If I add the WHERE clause it only returns the first record in the subform regardless of join type.
SELECT SubrecipientIDSheet.ID, SubrecipientIDSheet.AgencyID, AgencyIDSheet.SubgranteeLegalName, SubrecipientIDSheet.IsAmendment, SubrecipientIDSheet.RecordDate, SubrecipientIDSheet.HasSubrecipient, SubrecipientIDSheet.IsActiveSub, SubrecipientIDSheet.SubrecipientName, SubrecipientIDSheet.SubrepAddress, SubrecipientIDSheet.SubrepEHA, SubrecipientIDSheet.SubrepEHAVet, SubrecipientIDSheet.SubrepERA, SubrecipientIDSheet.SubrepESG, SubrecipientIDSheet.SubrepHSP, SubrecipientIDSheet.SubrepHTBA, SubrecipientIDSheet.SubrepLIHRF, SubrecipientIDSheet.SubrepSHAP, SubrecipientIDSheet.SubrepCounty1, SubrecipientIDSheet.SubrepCounty2, SubrecipientIDSheet.SubrepCounty3, SubrecipientIDSheet.SubrepAuthorizedRepresentative, SubrecipientIDSheet.SubrepRepTitle, SubrecipientIDSheet.SubrepEmail, SubrecipientIDSheet.SubrepPhone, SubrecipientIDSheet.SubrepTargetPop1, SubrecipientIDSheet.SubrepTargetPop2, SubrecipientIDSheet.SubrepTargetPop3, SubrecipientIDSheet.SubrepTargetPop4, SubrecipientIDSheet.SubrepOtherTargetPop, SubrecipientIDSheet.SubrepPrimaryService, SubrecipientIDSheet.SubrepCulturalSpecificPop, SubrecipientIDSheet.SubrepUseHMIS, SubrecipientIDSheet.SubrepHMISSupportPlan, SubrecipientIDSheet.SubrepDataReportMethod, SubrecipientIDSheet.DescripCurrentScope, SubrecipientIDSheet.DescripChangeScop, SubrecipientIDSheet.DescripWhyChange
FROM AgencyIDSheet INNER JOIN SubrecipientIDSheet ON AgencyIDSheet.ID = SubrecipientIDSheet.AgencyID
WHERE (SubrecipientIDSheet.ID) = [Forms]![Subrecipient Information].[Form]![SubrepSubform].[Form]![ID];

How to make a lookup query to another table filter out records based on existing relationships between tables without macros?

Given those relationships, how do I limit the choice of Leader in a given record in GroupResults to only those StudentResults.IDs, which have Class&Group set to the same value as in the ID field of that record without creating forms and using VBA?
If I assign SELECT StudentResults.ID, StudentResults.FullName FROM StudentResults; to the Row Source in [Leader], like this ,
I get all the records in the table to choose from, regardless of the [Class&Group] field value, like this .
How do I restrict the assignable records to only those that belong to the corresponding group?
I'd spent a very long time trying to find a way to run a parametrised SQL query to pass the [Class&Group] to the WHERE clause, but eventually had to give up.
Thank you very much for your help!
P.S. I do realise that this may or may not be more of an ms-access, rather than SQL question.
Tables are not designed to be user interfaces. Conditional comboboxes, validation, etc. work best on forms. Comboxbox lookup dropdowns are more an Access GUI convenience to show parent table indicators for key number values.
When queries are then run from such tables, these drop downs fields show to help us humans who naturally understand names and indicators rather than integer primary/foreign keys. So instead of Student: 1, we see Student: John Doe. In fact, such table field drop downs even helps generate the same comboboxes on Access forms and reports in advance to avoid the designer in building them upon clicking the form icons on ribbon.
However, for your needs consider adjusting combobox by showing the [Class&Group] field so the user can see or match the group of specific Leader with appropriate one for current record in Class column. See adjusted query and column count/heads.
Row Source: SELECT s.ID, s.[Class&Group], s.FullName FROM StudentResults s
Bound Column: 1
Column Count: 3
Column Heads: Yes
Also, if you want the Leader name to always show when table or query is opened instead of ID, reverse the order in query and change bound column:
Row Source: SELECT s.FullName, s.[Class&Group], s.ID FROM StudentResults
Bound Column: 3
Column Count: 3
Column Heads: Yes

Changing Row Source of a Lookup depending on another field

I'm trying to define the row source of a lookup field by selecting the table name from a separate lookup box.
The catalog of products comprises of about 41 Product Groups, which are then further divided into Types, some of which have over 100 types.
I have a table of Product Groups (41 groups), and I then have a separate table of Types for each Product Group (41 tables). All Type table names are exactly as they appear on the Product Group table. I want to be able to select the Product Group from a Lookup Box, and then select the Type from the corresponding table in a separate lookup box.
The images below should help give an idea of what I'm looking to do.
Set up of my first lookup box:
Set up of my second lookup:
Is this possible, and if so can anyone lend a hand ?
Thanks.
Just to summarize, you will need a single table with your TypeID, GroupID, and any other "Type" related fields.
Your Group ComboBox should have the ID field as its first column (makes the filtering much easier), So your control source should be:
SELECT [ProductGroup]![GroupID], [ProductGroup]![ProductGroup]
FROM [ProductGroup]
ORDER BY [ProductGroup];
Then in the properties for Group ComboBox on the Format tab make your column width 0";x" to hide the ID field.
The control source for the Type ComboBox should be:
SELECT [NewTypeTable]![TypeID], [NewTypeTable]![TypeName]
FROM [NewTypeTable]
WHERE [NewTypeTable]![GroupID] Like [Forms]![frmWithComboBoxName]![CboPGroup]
ORDER BY [NewTypeTable]![TypeName];
And again, if you want to hide the ID field, make the first column width 0".
You should also requery the second combobox in the afterUpdate() event of CboPGroup which will filter the second combobox based on the new selection in CboPGroup. The code (VBA) for that would be:
Forms!frmWithComboBoxName!CboType.Requery

Add a specified # of record to MSAccess Table Automatically

Happy Holidays. I have two dependent tables, [orders] and [reviews], linked by a "one to many relationship". On the [Orders], the PK is [Order#], there is a column for [#_of_reviews_ordered]. On the [reviews] table (the PK is an auto number) the linked field is [order#] and the number of records (records on the table) should equal "[Orders].[#_of_reviews_ordered]".
Is there a simple way to accomplish this without having to do add the records to [reviews] manually?
The only way I can think to do this without VBA is fairly convoluted and would only work if your number of reviews orders fits within a finite (and reasonably small) range. For my explanation I will assume # of review will be between 0 and 3
You would need to create a table called, say, TemplateReviews. This would have at least one field called "KeyNumber", which should not actually be a key. You could also repeat as many fields as desired from Reviews, and use them to store default values for the rows to be inserted.
The important thing about TemplateReviews is that you must set it up in advance to have N rows with KeyNumber=N for each possible value of KeyNumber. For my example, we can have 0 to 3 # of reviews. So TemplateReviews will have:
0 rows with KeyNumber=0
1 row with KeyNumber=1
2 rows with KeyNumber=2
3 rows with KeyNumber=3
Once you have TemplateReviews set up, you need to create an Insert query based on it. The query will insert rows from TemplateReviews into Reviews. But you also have to filter KeyNumber to match the value on the currently selected Order, as in
=Forms!Orders![#_of_reviews]
You then need run Insert query to run using a macro triggered by a button (etc) on the Orders form. This only works the first time you click the button... but you can modify the criteria expression above to subtract the number of existing reviews, as in
=Forms!Orders![#_of_reviews] - DCount("*","Reviews","OrderId=" & Forms!Orders![order#])
Hope this helps. If you got this approach working, you could then replace the button with a single line of VBA code in the Order form AfterUpdate event to trigger the insert query.