Access Userform: How best to show fields from a lookup table? - ms-access

Table1 has a field ItemID; I use that to lookup the corresponding record in Table2, which has fields ItemID and ItemDescription.
My form uses Table1 as its Recordsource. I want to display ItemDescription from Table2 in a combobox or textbox.
From what I've read, there are several ways to accomplish this, but I can't tell which is considered the standard method:
Add a subform to the form, with Table2 as its Recordsource.
Make a query combining Table1 and Table2, and use the query as the form's Recordsource.
Use a SELECT statement as Table1's Recordsource that pulls in fields from both tables. (Basically, using the aforementioned query in a different way.)
Use DLOOKUP as the Control Source of the textbox, something like
=Dlookup("ItemDescription","Table2","ItemID = Forms!MyForm!ItemID"
In my case, I expect I'll have to link Table1 to about five different lookup tables; and some of those may have lookup tables of their own.
What is the best way to show data on the form from these multiple tables?

Add a subform to the form, with Table2 as its Recordsource.
This is an overkill. Do this only for detail records where the main form is on the one side of a one-to-n relation.
Make a query combining Table1 and Table2, and use the query as the form's Recordsource.
This works only one way and is okay for a read-only form. But you cannot edit the value and the value will not change when you make edits to another field containing the id
Use a SELECT statement as Table1's Recordsource that pulls in fields from both tables.
Same as above.
Use DLOOKUP as the Control Source of the textbox.
This works well and you can update the textbox with textbox.Requery() when necessary. But of course, you cannot edit the value.
An option that works for displaying AND for editing the value is to use a ComboBox. Setup the combo box like this:
(in the Data section) Set the Control Source to ItemID. This is a column of the Record Source of the form.
Set the Row Source Type to Table/Query
Either specify a query name or a SELECT statement as Row Source. Select the Id as first column and the display name as the second. E.g.
SELECT ItemID, ItemDescription FROM Table2 ORDER BY ItemDescription;
Set the Bound Column to 1 (the ID column)
Set Limit To List to Yes
(in the Format section) Set Column Count to 2
Set Column Widths to 0 to hide the first column with the Id. You can specify a comma or semicolon delimited list of column widths here if you want to display several columns.
You can specify a higher number of rows in ListRows than the default 8 if you want.
Now the combo box displays ItemDescriptions automatically and is editable. I.e. you can select another Item from the drop down displaying ItemDescriptions and the ItemID on the form will be updated.

Related

What is the difference between Control source and Bound Source in Combo/List Box?

In the Property Sheet for Combo / List Box, we have Control Source and we also have the bound column.
How is control Source different from bound column?
The Control Source is the field in a table your combo box is linked to, it could also be a query or an SQL statement. Either way it will display the data in that field and any updates you make to the combo box will be reflected in that field (unless it's a non-update-able query).
https://support.office.com/en-gb/article/ControlSource-Property-994c3208-c4e5-431d-8ec8-dabd5f91c77e
The Bound Column is linked to the Row Source. The Row Source tells the combo box what data is supplied to the combo box. Generally this will be the primary key of a list and a description of the items in that list, for example PersonID and PersonName.
https://msdn.microsoft.com/VBA/Access-VBA/articles/combobox-rowsource-property-access
The Bound Column tells the combo box which field of data in the Row Source to store in the Control Source field. So when you select PersonName the PersonID for that record will be stored.
https://msdn.microsoft.com/VBA/Access-VBA/articles/combobox-boundcolumn-property-access
Example
I have set up two tables in my database:
tbl_List contains four records used to populate our combo-box.
Main_Field_A in tbl_Main will hold the bound column value selected in the combo-box (this will be the Foreign Key linked to tbl_Lists Primary Key).
The Property Sheet for the combo-box is set up as below:
Note the Row Source Type is Table/Query.
We could type SELECT Field_A, Field_B FROM tbl_List; into the Row Source for the same result.
We could also manually enter the items in the list by typing 1;Item1;2;Item2;3;Item3;4;Item4 and changing the Row Source Type to Value List
In addition the Format tab has the Column Count set to 2 and the Column Widths set to 1cm;1cm. Usually the first, or bound column will have it's width set to 0cm so it is hidden.
In the below example I've changed the column widths to 0cm;1cm so the bound column is hidden.
As you can see it's entered the Primary Key value into the table, rather than the description.
The query to return the record would be:
SELECT Main_Field_ID
, Field_B
, Main_Field_B
FROM tbl_Main INNER JOIN tbl_List ON tbl_Main.Main_Field_ID = tbl_List.Field_A
or you could use the Lookup Field functionality in Access, but that leads to the darkside.
http://access.mvps.org/access/lookupfields.htm

access combo box on a form to display text values from table/query and save the associated ID value in the table on my form is already based on

My project is a form based on a transaction table, this form has four combo boxes. Basically all of them are storing ID values, and the question is how to make access display the associated value of the ID on the form, while the ID# is being stored in the table.
Example: one combo name is well_ID on the form, which will be populated from the table (Wells) by select staetment [SELECT Wells.Well_ID, Wells.Wellname FROM Wells]
i need the combo to display the well name and store the well_ID in the table.
Would you please help on this.
Regards
Mohamed
The easiest way to do this is to use the wizard and let it guide you through. Create a form based on a table or query, ensure the wizard button is clicked, add a combo and follow the steps provided by the wizard.
You will end up with something on these lines:
Row Source Type : Table/Query
Row Source : SELECT Wells.Well_ID, Wells.Wellname FROM Wells
Bound Column : 1
Column Count : 2
Column Widths : 0cm;2cm;
If you choose to store the value in a field (column), you will need:
Control Source : SomeID
Which refers to the field or column on in the table or query bound to the form and takes its contents from the Bound Column, which can be any valid column number, starting from one (1). In other words, what ever field in that table that should store Well_ID.
The Row Source can be a more complicated SQL string, a query or table. It can refer to other controls on any open form. These properties can be set by code.

Access: Select multiple records to print report

I am trying to design a form where I can select multiple records and on button click open the report with the selected records IDs.
I am thinking maybe make a continuous form with an added unbound check box control where the user selects the appropriate records they want to display in the report. I am unsure how to later read this into a do.cmd OpenReport criteria property.
If there is an easier way to this please let me know.
Take a look at http://support.microsoft.com/kb/135546 or http://allenbrowne.com/ser-50.html. Another approach you could take is create a temporary table that contains a single field for your record ID. Using the code from the links, fill the temporary table with the record IDs that have been selected. Then all you need to do is change the query that drives your report to do an inner join with the temporary table.
Can you add a column to your dataset where they can determine which rows to print? You could add a printMe Y/N column, for example, and then use that field value to limit the rows for the report.

Access 2007: Looking up data in one table based on selection in a combo box

I'm currently working on a project where I'd like to display all the data from a given table based on a selection made in a Combo Box.
What I have currently is the following:
Four tables (T1, T2, T3, T4) each with two columns of data (Values1, Values2).
One combo box which contains the names of these look up tables. The user selects one of these options to display the data from the specific table.
Two other combo boxes, where I'd like to place the data from the chosen table into.
If I were writing C# I could do something like:
String query = "SELECT (Values1, Values2) FROM " + TableName;
var rows = doQueryAndGetRows(query);
displayMyrows(rows);
How can I do something similar in Access? I've never really used it before, and I don't have much experience with VB for applications either.
In Access you can have cascading combo boxes, which would seem to be what you want for your additional combos, subforms, which would seem to be where you wish to display your data, and a number of other easy ways to do what you wish.
For example, in the After Update event of your combo box, you could simply set the Record Source of a subform to the table name:
Me.MySubformControl.Form.recordSource = Me.MyCombo
Assuming that the bound column of the combo contains the table name, or using the column property, if some other column contains the name.
As for cascading combo boxes: Is there a simple way of populating dropdown in this Access Database schema? or http://support.microsoft.com/kb/289670

How do I make foreign-key combo boxes user-friendly on an Access form?

I've got two tables:
Employees:
uid (number) | first_name (string) | last_name (string) | ...
Projects:
uid | project_title (string) | point_of_contact_id (FK: Employees.uid) | ...
I'd like to create a form for Projects with a "Point of Contact" combo box (dropdown) field. The display values should be "first_name last_name" but the backing data is the UID. How do I set up the form to show one thing to the user and save another thing to the table?
I'd be fine with only being able to show one field (just "first_name" for example), since I can create a view with a full_name field.
Later:
If there is a way to do this at the table design level, I would prefer that, since then I would only have to set a setting per UID column (and there are many tables), rather than one setting per UID field (and there are many forms, each with several UID fields).
To expand on Loesje's answer, you use the Bound Column property along with Column Count and Column Widths when you are displaying multiple fields so that you can tell Access which one should be written to the database. (There are other ways to do this using VBA, but this should work for your particular case.)
In your case, setting Row Source to select uid, first_name, last_name from tablename means that your Bound Column should be 1, for the first column in your Row Source (uid). This is the default value, so you'd only have to change it if you wanted to save a value from a different field. (For example, if you wanted to save last_name from the Row Source above, you'd set Bound Column to 3.)
Don't forget that when you set the widths of the other columns you are displaying, the combo box's Width property should be greater than or equal to the sum of the column widths, otherwise you may not see all the columns.
There isn't a way to indicate at the table level that a form based on that table needs to pull particular columns, or that a particular column is the foreign key, but you can copy a combo box to other forms and it will carry all its properties with it. You can also copy the Row Source query and paste it into other combo boxes if that helps you.
Set the rowsource of the dropdownbox to "select uid,first_name,lastname from tablename' and set the columnwidth to 0. This way the width of the first column is set to zero, so the user doesn't see it. (You can provide the width of the other columns seperating them with a semicolon, ie: 0cm;4cm;4cm)