Changing Row Source of a Lookup depending on another field - sql

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

Related

How do I create a form to input new records into a table that contains only ID values from other tables?

I am new to databases, and I am working on a final project for a class. I have a database with tables related to each other as shown in the diagram here:
I want to create an unbound form that allows a user to add a new purchase to the purchase table by choosing their name, category, and store from data already in these tables, and then add purchase amount and date.
Since the purchases table does not contain the names of people, categories, and stores themselves, only the ID values from these tables' fields, I am struggling with how to create a form that will add the correct IDs into a new record in Purchases based on the names from other tables.
I am wondering if this requires VBA? I have tried playing around with the property sheet on forms, but I am struggling with which properties to address/what to do with them.
If anyone can explain at least a starting process to create this form.
Simply, use combo boxes that query Buyer, Category, and Store table data, hides the primary key ids, but shows the corresponding lookup value to the user. Users will select by the lookup value(s) but really are saving the id to Purchases table as new foreign key id.
As commented above, use a bounded form to map combo boxes to table id fields. Once you place a combo box, the default wizard will guide you on the steps but below are key property sheet attributes (which may be automatically set with wizard but can be manually adjusted later).
Data
Control Source: The column in table (i.e., PurchaseID, BuyerID, CatID, StoreID) to store the user-selected data of combo box (i.e., a form control).
Row source: A distinct SQL query of primary table id and all needed values for human searches. This can be a named table or saved query or an inline SELECT statement.
Row Source Type: If using SQL, Table/Query.
Bound Column: The position of primary table ID in the query resultset to be stored as foreign key Id. Usually this would be the first column.
Format
Column Count: The total number of columns from the recordsource including hidden, bound column.
Column Widths: To hide column from view, set its positional number within semicolon delimiters to zero. Preview form to decide how large to space out other columns. Do note: you can extend beyond the Width of combo box using List Width.
Column Heads: Optional and best if more than one column to guide users on the lookup value content (e.g., First Name, Last Name).
As example, for Category combo box on bounded Purchases table form, consider below property values:
Control Source: CatId
Row Source: SELECT CatId, CatName FROM Category
Row Source Type: Table/Query
Bound Column: 1
Column Count: 2
Column Width: 0";2.5"
Column Heads: No

Return subform values based on muliple criteria in Access

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.

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

How do I select data from the current row in sql and access

Think this may be an obvious question but have had a search and still cant seem to get this to work.
Basically I am trying to knock up a quick access database which will allow one field to limit what is shown in the next field.
So if the user selects a categories in one field it will then limit a subset of subcatories in the next field.
The way i have it set up for this part is I have a a mastertable which is what the user will use and then I have another table which contains each category and subcategory
ie.
category1 subcat1
category1 subcat2
category2 subcat1
etc.
How can I achieve this in access as I am unsure of how to select the current row data to use in the query.
This is what code i have but it doesn't work.
SELECT [Industry types].[Industry types]
FROM [Industry types], MasterTable
WHERE [Industry types].[Main Category] = [MasterTabel]![Industry Category 1];
Thanks in advance!
A query doesn't have an actual record.
You must display the information of the master table in a form and use a subform that will show the related records. The RecordSource of the main form must be the MasterTable or a query based on it. The subform's RecordSource must be [Industry types] table or a query based on it. You don't need a where clause, instead, you set the subform's LinkChildFields property to Main Category and LinkMasterFileds property to Industry Category 1.
See Create a form that contains a subform (a one-to-many form)

Get specific cell in Crystal Report

I have a Crystal Report linked to table Customer in SQL Server database. My report generator will execute SELECT SQL and pass the result table into the report as data source.
In the report, I have a field and I want this field to display the cell data at the specific row index and column index of the table (maybe I know the column name). For example, my field should display cell at row 3, column 2 of the data source.
How can I do this using Crystal Report. The latest version now is 2011.
First off, in order for the row number to have any meaning, you'll need to be querying the table with an ORDER BY clause (by sorting the report). Without it, you can't make any assumptions about the "original order of records" in the DB as there really is none.
There are a few ways to get this field depending on where in the report you want to display it. If you simply want to show it in the details section, you can make use of a simple formula like this
if rownumber = 3 then {table.column}
If you'd like to display it instead in a footer, you could make use of a variable instead:
whileprintingrecords;
numbervar thedatavariable;
if recordnumber = 3 then thedatavariable := {table.column}
Throw that formula in the details section of the report and you're then free to use thedatavariable in your footers.
Now for the column: If the column index is NOT dynamic you can just see which column corresponds... for example, if the table's columns are customer_id, customer_name then column 2 will always just be the customer name. If the index number will change, like via a parameter, you could make a formula like this
select {?colIndexParameter}
case 1 : {table.customer_id}
case 2 : {table.customer_name} ...