Primary Keys and Foreign Keys - Kick me out of the Access club

D

Dazed And Confused

This question is so fundamental that Microsoft will probably draw up a
court order that forbids me from coming within 10 feet of Access ever
again. I feel really stupid, but I gotta figure this out and I'm not
having any luck on my own.

I have done a lot of querying data warehouses with Access, but not
much designing databases except to create flat files. Now I need to
start creating some relational databases.

Lets say I have a table called Customers. My fields are CustomerID
(autonumber), CustomerName, and CustomerAddress. CustomerID is the
primary key.

I also have a table called Services. My fields are ServiceID
(autonumber), Service_Name, Service_Date, and CustomerID. ServiceID
is the primay key and CustomerID is a foreign key (I guess).

When I am creating new records for the Services table, how do I know
what CustomerID number to use so that it matches the CustomerID in the
Customer table?

As embarassing as it is, I do not know how to make the Key field in
the Customers table equal the foreign Key in the Services table. I'm
not even sure I am saying this correctly.

When I am populating the Services table, how do I know which
CustomerID to use?

So. After everyone has had a good laugh at my ignorance, would some
kind soul out there take pity on me and help me figure this out?

Thank you for your time! I appreciate it.
 
D

Duane Hookom

Don't be so tough on yourself. Rule number 1 is to always perform record
entry and editing in forms. You create a form based on your Services table.
Use the combo box wizard to bind to the CustomerID field in Services but
select values from the Customers table.

You might want to open the Northwind sample database and look as some forms
for order entry or whatever.
 
J

John W. Vinson

I also have a table called Services. My fields are ServiceID
(autonumber), Service_Name, Service_Date, and CustomerID. ServiceID
is the primay key and CustomerID is a foreign key (I guess).

When I am creating new records for the Services table, how do I know
what CustomerID number to use so that it matches the CustomerID in the
Customer table?

The simplest way is to use a Form to populate your tables (you certainly
should not be working in table datasheets!); you can create a Form based on
Customers, with a Subform based on Services. A Subform control has a master
and child link field property; set these both to CustomerID. New records added
to the subform will inherit the CustomerID from the current record on the
mainform; and as you navigate from record to record, the subform will display
only those services linked to the current CustomerID.

John W. Vinson [MVP]
 
T

Tony Toews [MVP]

Dazed And Confused said:
This question is so fundamental that Microsoft will probably draw up a
court order that forbids me from coming within 10 feet of Access ever
again. I feel really stupid, but I gotta figure this out and I'm not
having any luck on my own.

This is absolutely *NOT* a stupid question. You're not the only person
to ask such a question. And there are likely hundreds of people who
will be grateful you asked such a question as they may have been too
bashful to ask themselves.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 
C

Cheese_whiz

Hi Dazed,

The simple answer is that, in the form where you add new service records,
you have a combo box where you select customers. The combo box lists
customers, by name, alphabetically. The user selects a customer name.

'Hidden' inside the combo box's functionality is the fact that, for each
customer name displayed to the user, there is also the customerID for that
customer name. The user is selecting the customer name, but what's really
getting stored in the underlying record is the customerID associated with
that customer name. The net effect is that the user gets something to work
with he understands (customer names), and access gets something to work with
that's the most efficient way to represent and save customers to records
(customerID).

To accomplish that, you use an sql select query as the rowsource of a combo
box. You set the rowsource type to 'table/query'. You set the column count
to '2', you set the 'bound column' to '1' (in the example that follows), and
you set the 'column widths' to '0;1"' (remove all SINGLE quotes from the
previous sentence when inputing values).

You would, of course, set the 'recordsource' of the form to a table or
query, and the 'control source' of the combo box to the field in that
table/query that represents the customerID data.

All those settings are in the properties window, and all but the
'recordsource' are in the properties window related to the combo box. You
get the properties window open by hitting the f4 key. Open your form in
design mode, hit f4, and you open the properties window with the form itself
as target of the properties window. That means all the properties shown in
the properties window relate to the form itself (the form as an entity in and
of itself). If, in design mode, you select the combo box control, then open
the properties window (or, if you already had the properties window open and
select the combo box control), then all the properties you'll see in the
properties window relate to the combo box.

Back to the rowsource. A select query used for the rowsource, given the
example you gave of your customers table, would look like this:

Select CustomerID, CustomerName from Customers Order by CustomerName;

That is, in the real world, most likely a bit simplistic because you've got
a field called 'customerName' in the customer table when, in reality, you'd
likely have separate fields for first name, last name, middle name. A more
realistic sql select statement might look like this:

Select CustomerID, Trim(LastName & ", " & FirstName & " " & MiddleName) As
CustomerName from Customers Order by CustomerName, CustomerID;

That produces a "firstname, lastname middlename" formatted customer name
list (along with their customerID numbers).

Anywho, so you stick that select statement into the 'rowsource' of the combo
box, and make all the other settings as stated above, and now you have a
combo box control that will store an accurate list of your current customers,
display the list using the customer names, but store the choices made by the
user utilizing the customerID.

Neat, huh?

At some point, when you get all that working, then you'll have to deal with
the situation where you need to add a new customer. You can do a search for
"NotInList" events and find code that will help you address that situation.

Bear in mind: IF you've already designated the customer in question in
another form that's currently open, you can simply pull that customer data
from one form to the other (second) form. That is NOT an uncommon situation,
so think about whether or not your situation requires a CHOICE, or if your
situation requires an ACKNOWLEDGEMENT of a PREVIOUSLY MADE choice (AND the
result of that previous choice is easily accessible to you).

It bascially breaks on this assessment: In a 'main' type form where you are
creating independent/New/main records, you'll definitely need a combo box.
For example, if your application is an orders tracker, and each order
requires you to add the related customer to it, then you'll need a combo.

In a form that creates RELATED records to a main record (displayed in a form
currently opened), you often can skip the combo box and just pull the choice
from the main form to a hidden control in the related form.

That is confusing, I realize. Here's an example:

If you have a 'files' database, and you want to relate a list of people to a
file. Your main record is 'file', your related record is FilePerson So the
files table has a one-to-many relationship with this second table that will
store people related to the file.

Well, to add a person to a file, you don't need to have a combo box that
people use to select which file you are adding people to, because you will
open the file record in the 'main' form, then click on an 'add related
people' button on that main form that will open a (second) form to add the
people. The addRelatedPeople form will get the 'fileID' it needs from the
already opened main file form. You don't need to 'select' a file, because
that choice has been 'previously made' when you opened the main form to the
file in question.

In contrast, you WILL need a combo box on the second (addRelatedPeople) form
for users to select the person they want to add to the file. Why? Because
that choice has NOT already been made (it is, in fact, the whole point of the
second form).

Wow, that answer got a bit out of hand...hope it's not too confusing.

Anway, hope you find some value in here.
CW
 
D

Dazed And Confused

Hi Dazed,

The simple answer is that, in the form where you add new service records,
you have a combo box where you select customers. The combo box lists
customers, by name, alphabetically. The user selects a customer name.

'Hidden' inside the combo box's functionality is the fact that, for each
customer name displayed to the user, there is also the customerID for that
customer name. The user is selecting the customer name, but what's really
getting stored in the underlying record is the customerID associated with
that customer name. The net effect is that the user gets something to work
with he understands (customer names), and access gets something to work with
that's the most efficient way to represent and save customers to records
(customerID).

To accomplish that, you use an sql select query as the rowsource of a combo
box. You set the rowsource type to 'table/query'. You set the column count
to '2', you set the 'bound column' to '1' (in the example that follows), and
you set the 'column widths' to '0;1"' (remove all SINGLE quotes from the
previous sentence when inputing values).

You would, of course, set the 'recordsource' of the form to a table or
query, and the 'control source' of the combo box to the field in that
table/query that represents the customerID data.

All those settings are in the properties window, and all but the
'recordsource' are in the properties window related to the combo box. You
get the properties window open by hitting the f4 key. Open your form in
design mode, hit f4, and you open the properties window with the form itself
as target of the properties window. That means all the properties shown in
the properties window relate to the form itself (the form as an entity in and
of itself). If, in design mode, you select the combo box control, then open
the properties window (or, if you already had the properties window open and
select the combo box control), then all the properties you'll see in the
properties window relate to the combo box.

Back to the rowsource. A select query used for the rowsource, given the
example you gave of your customers table, would look like this:

Select CustomerID, CustomerName from Customers Order by CustomerName;

That is, in the real world, most likely a bit simplistic because you've got
a field called 'customerName' in the customer table when, in reality, you'd
likely have separate fields for first name, last name, middle name. A more
realistic sql select statement might look like this:

Select CustomerID, Trim(LastName & ", " & FirstName & " " & MiddleName) As
CustomerName from Customers Order by CustomerName, CustomerID;

That produces a "firstname, lastname middlename" formatted customer name
list (along with their customerID numbers).

Anywho, so you stick that select statement into the 'rowsource' of the combo
box, and make all the other settings as stated above, and now you have a
combo box control that will store an accurate list of your current customers,
display the list using the customer names, but store the choices made by the
user utilizing the customerID.

Neat, huh?

At some point, when you get all that working, then you'll have to deal with
the situation where you need to add a new customer. You can do a search for
"NotInList" events and find code that will help you address that situation.

Bear in mind: IF you've already designated the customer in question in
another form that's currently open, you can simply pull that customer data
from one form to the other (second) form. That is NOT an uncommon situation,
so think about whether or not your situation requires a CHOICE, or if your
situation requires an ACKNOWLEDGEMENT of a PREVIOUSLY MADE choice (AND the
result of that previous choice is easily accessible to you).

It bascially breaks on this assessment: In a 'main' type form where you are
creating independent/New/main records, you'll definitely need a combo box.
For example, if your application is an orders tracker, and each order
requires you to add the related customer to it, then you'll need a combo.

In a form that creates RELATED records to a main record (displayed in a form
currently opened), you often can skip the combo box and just pull the choice
from the main form to a hidden control in the related form.

That is confusing, I realize. Here's an example:

If you have a 'files' database, and you want to relate a list of people to a
file. Your main record is 'file', your related record is FilePerson So the
files table has a one-to-many relationship with this second table that will
store people related to the file.

Well, to add a person to a file, you don't need to have a combo box that
people use to select which file you are adding people to, because you will
open the file record in the 'main' form, then click on an 'add related
people' button on that main form that will open a (second) form to add the
people. The addRelatedPeople form will get the 'fileID' it needs from the
already opened main file form. You don't need to 'select' a file, because
that choice has been 'previously made' when you opened the main form to the
file in question.

In contrast, you WILL need a combo box on the second (addRelatedPeople) form
for users to select the person they want to add to the file. Why? Because
that choice has NOT already been made (it is, in fact, the whole point of the
second form).

Wow, that answer got a bit out of hand...hope it's not too confusing.

Anway, hope you find some value in here.
CW



As embarassing as it is, I do not know how to make the Key field in
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top