Question

I have a few tables set up in Access as follows (forgive the slightly redundant example content):

Table 1:
- ID
- FirstName
- SecondName

Table 2:
- ID
- Details
- PersonID -> Table 1[ID]

Table 3:
- ID
- Group
- PersonDetails -> Table 2[ID]

Table 1 is the base table containing records and retrieving no other information. For example, it could store someone's first and second names, along with an autonumber ID.

Table 2 contains records which, amongst other things, contain a field that links to Table 1 and stores the ID of one of the records held there. With the lookup wizard I can choose to utilise all fields from Table 1, store the ID of the Table 1 record in the Table 2 field and also display the first and second names in the combobox on the form to make choosing a record more intuitive.

In table 3, I need to store the ID of one of the records in Table 2. However, I would also like to again display in the form combobox the first and second names from the related record (in Table 1) whose ID is stored in Table 2. I can't choose to utilise, for example, the PersonDetails field from table 2 as this just puts ID numbers into the combobox - I'd need to do something equivalent of:

Table 2[ID]->[FirstName]

Is this possible to do with the lookup wizard in Access or would I have to look into queries or VBA?

Thanks.

Was it helpful?

Solution

Your query for your combo should look something like this:

SELECT cp.ID, cp.ReferenceName, c.Company, p.FeePerHour 
FROM (ClientProfile AS cp LEFT JOIN Clients AS c ON cp.ClientID = c.ID)
LEFT JOIN Pricing AS p ON cp.PricePlanID = p.ID;

The main problem with your query is that you're missing the Parenthesis that are needed when you have multiple joins. If you had another join, you'd need a second set of parenthesis.

I took some liberty and used table aliases. It makes SQL concise and more readable.

If this query still doesn't work it might be because you're trying to join "child tables" to the "main table" without using your Foreign Key fields as the joining field. If this were my database the SQL would look something like this:

SELECT cp.ClientProfileID, cp.ReferenceName, c.Company, p.FeePerHour 
FROM (ClientProfile AS cp LEFT JOIN Clients as C ON cp.ClientID = c.ClientID)
LEFT JOIN Pricing AS p ON cp.ClientProfileID = p.ClientProfileID;

Personally, I basically never use just plain ID as a field name. And when creating a foreign key I usually use the same field name as what the original table has. There are exceptions, especially in the case where you need to link back to the same table more than once. Consider the case where you are tracking real estate properties and you have a Buyer and a Seller that are both entities (but different from each other) in the same People table. You would then need to name them something like BuyerID and SellerID but ultimately they would link back to the PersonID in the Person table.

So Here's the table design I would go for. Notice I changed Group to PriceGroup. Watch out for reserved words when programming, not only in SQL but any language. If you name your field Group at some point you'll be trying to "Group on Group" which sounds and looks bad, if nothing else.

Client:
- ClientID
- FirstName
- SecondName

ClientProfile:
- ClientProfileID
- Details
- ClientID (Foreign Key to Client)

Pricing:
- PricingID
- PriceGroup
- ClientProfileID (Foreign Key to ClientProfile)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top