mark dixon page 1 05 – database design: sub-forms
Post on 18-Dec-2015
223 views
TRANSCRIPT
Mark Dixon Page 1
05 – Database Design: Sub-forms
Mark Dixon Page 2
Session Aims & Objectives• Aims
– To allow easier data entry from multiple tables using forms
• Objectives,by end of this week’s sessions, you should be able to:
– create a sub form that allows data entry into a related table
Mark Dixon Page 3
What is a Main and Subform?• In a One to Many relationship:
– Main form displays the ‘One’ record• Master table (or Query)
– Subform displays the ‘Many’ records• Child or Details table (or Query)
• Customer Order Details– Main contains Customers, Customer Orders and Order tables
– Subform contains Order Lines and Products tables
Mark Dixon Page 4
Sub-forms: Why?• Usually used for tables with a One-to-Many
relationship• Need to enter several records in one table link to
one in another• Wish to view all records from one table which are
related to one in another
Mark Dixon Page 5
Sub-forms: Why?• Creation of coherent
data– Ensures orders are
‘linked’ to the correct customer
• View data linked to a record– Check current orders– Check back orders
Mark Dixon Page 6
How do they work?• Main form produced from a table or query
– Displays data from the ‘one’ side of relationship
• Subform produced from different table or query– Displays data from the ‘many’ side of relationship
• Subform inserted into Main form– Subform only displays records linked to Main– All data available form that one-to-many
Mark Dixon Page 7
Creating Main and Subforms…
• Create query for Main form
• Create query for Subform(s)
• Create form layouts:– Main– Subform(s)
• Insert Subform(s) into Main form
• Link Forms• Final format to create
desired view
Mark Dixon Page 8
An Example…• ACME Customer
Order Details
• Select Customer– View items to send– Used by warehouse
to pick/send orders
Mark Dixon Page 9
Creating Main Form Query…
• Create query to join Customers, Customer Orders and Orders
Mark Dixon Page 10
Creating Subform Query…• Create query to join Order Lines and Products
Mark Dixon Page 11
Creating the Subform…• Open the form Wizard• Select the Sub Form Query from ‘Table/Query’
• Use all fields• Select ‘by Order Line’
Mark Dixon Page 12
Creating the Subform…• Select ‘Datasheet’• Select ‘Standard View’• Open in ‘Design View’• Field label should be
meaningful:– Change Caption of Label– Change Name of Field
Mark Dixon Page 13
Creating the Subform…• Change field order
to make logical sequence
• Change order in Design View
• Change Tab Order– Right Click
Available‘Areas’
Fields andcurrent order
Mark Dixon Page 14
Creating the Subform…• Hide unneeded
columns– Order ID is not
needed in Subform as it is stated in Main Form
• In ‘View’ right click on the column
• Select ‘Hide Columns’
• Save form
Mark Dixon Page 15
Creating the Subform…• Final form will look like this
– Field titles and order are as desired– All Order Lines records are shown as it is not
yet linked to the Main Form
Mark Dixon Page 16
Creating the Main form…• Open the form Wizard• Select the Main Form Query form ‘Table/Query’
• Use all Fields
• Select ‘by Orders’– As we wish to view the
orders
Mark Dixon Page 17
Creating the Main form…• Select ‘Column’ layout• Select ‘Standard’ style• Save form as “Order Details”
• Open in ‘Design View’
• Format fields and labels
Mark Dixon Page 18
Linking Main and Sub• Extend the main
forms detail area• Move form so you
can see the objects window
• Select and drag the sub form in and drop
• Select the subform and open properties
Mark Dixon Page 19
Creating the Main form…• Add header to ‘explain’
form• Add label to ‘explain’
Subform• With Main and Subform
Linked only records that are linked will be shown
• Save Form
Mark Dixon Page 20
Result of Main and Subform…
Mark Dixon Page 21
Tutorial Exercise: Music• Task 1: Use a form and sub-form to enter
data for Tracks and Artists