|
5. Related Tables/ Normalized Database
This follows the Customers & Orders example given in the documentation, and shows you how to make the related records using that data source.
In the example, there are two tables. One is called Customers, and it has a primary key field called "CustomerID". The second table is called "Orders" and has a foreign key that matches the Customers. CustomerID key called "CustID". What we want to do is set up a DBList for the Customers table that will show the related records in the Order table. This list of the related orders will be on another page with another DBList.
Set up the page with the Customer table.
- As with all ASP pages, the file extension needs to be set to .asp, and the "Layout is a form" checkbox needs to be selected. Like most pages with a DBList component, this page has have three components:
-- an MSDBConnection component to connect to the database -- an MSDBQuery component to select the customer records -- an MSDBList component to display the customer records.
- Set up your MSDBConnection component to point at your Microsoft Access data source.
- In the MSDBQuery component only needs to "SELECT * FROM Customers". Notice you do not have to set up a connection to the Orders table here.
- In the MSDBList component is where you establish the link between DBList and the DBList that will display the Orders records. Set the "Hyperlink Page" field to the page where you want to display your Orders records. For this example, use a page called "MyOrders".
- For "Hyperlink Field" put in "CompanyName". This is the field in the Customers table where you want to display the hyperlink.
- Set the "Key Field Name" and "Key Field Label" fields to CustomerID. Set the "Key Field Type" to number.
NOTE: This is the critical part of the equation. The "Key Field Name", "Key Field Label" and "Key Field Type" fields define what field is passed to the Hyperlink page. Since the foreign key in the Orders table, Orders.CustID, needs to match the value Customer.CustomerID, you want to "broadcast" the Customer.CustomerID field to the "MyOrders" page. This is similar to previous configurations except that now you know that "Key Field Name" and "Key Field Label" are what field is sent to the Hyperlinked page.
Now set up the "MyOrders" page to display the related records.
- On your "MyOrders" page, once again make sure the page extension is set to "asp" and that the "Layout is a form" checkbox is checked.
- Place a MSDBConnection, MSDBQuery and MSDBList component on this page.
NOTE: The MSDBConnection on the "MyOrders" page still just points to your Microsoft Access source. The MSDBQuery is the critical part of this page. For this example this query is named "MyRelatedQuery", and you should leave the "SELECT" field set to "*".
- Set the "FROM" field to "Customers, Orders".
- Set the "WHERE" field to "Customers.CustomerID=Orders.CustID".
NOTE: In the "WHERE" field is where the actual logic occurs. Since the "Customers.CustomerID" field was passed over from the previous page using the "Key Field Name" field, "myRelatedQuery" knows about its existence. Since this query knows about the Orders table, it knows about its fields. Because of this, the "Customers.CustomerID=Orders.CustID" in the WHERE field can make sure that only matching fields are displayed.
- Place a DBList component that lists all the fields from the Orders table that you would like to display. When you publish this site, the Customers page displays all the customers, and if you click on the hyperlink, it shows all the orders that belong to that customer.

|