05 access howtolookupselect

28
MS Access How To: Look up and select values Unit 18 Database Design Extended Diploma in ICT

Transcript of 05 access howtolookupselect

Page 1: 05 access howtolookupselect

MS Access How To:Look up and select values

Unit 18 Database DesignExtended Diploma in ICT

Page 2: 05 access howtolookupselect

Link tables• A link table will have foreign keys• To create a record you want to select a value from another table and put the key value in the link table• You do this using list boxes

Page 3: 05 access howtolookupselect

Example• You want to select a customer, then a costume and put the key

values for those selections into the rental table

Page 4: 05 access howtolookupselect

Create a form• Make a form for the link table• Add the foreign key IDs

Page 5: 05 access howtolookupselect

Add a list box• Click on the List Box control and drag out a list box• The List Box Wizard starts

List box control

Page 6: 05 access howtolookupselect

List box wizard• Choose the list box to look up the values in a table or query

Page 7: 05 access howtolookupselect

List box wizard• Select the table you will look up

Page 8: 05 access howtolookupselect

List box wizard• Choose the fields that will appear in the list

box• Pick the primary key first

Page 9: 05 access howtolookupselect

List box wizard• Choose any sorting that you want

Page 10: 05 access howtolookupselect

List box wizard• You are shown a preview

Page 11: 05 access howtolookupselect

• You now choose to save the primary key value from the list box in the foreign key field on the form

Page 12: 05 access howtolookupselect

List box wizard• Type a label for the list box

Page 13: 05 access howtolookupselect

List box wizard• The wizard closes and the list box allows you

to select a customer and put the ID in the correct field

Page 14: 05 access howtolookupselect

Adding a subform to show more detail• There are more customer details you may

want to show, such as the address. • You can use a subform for this• Click on the subform control and draw out a

subform

Subform control

Page 15: 05 access howtolookupselect

Subform• The subform wizard opens• Choose to use an existing table or query

Page 16: 05 access howtolookupselect

Subform• Select the right table and the fields you want

to show• You must include the key

Page 17: 05 access howtolookupselect

Subform• Choose from the list and the subform will

show the values from the table associated with the primary key in the link table

Page 18: 05 access howtolookupselect

Subform• Give the subform a sensible name

Page 19: 05 access howtolookupselect

Subform• The subfom is created (and looks awful)

Page 20: 05 access howtolookupselect

Subform appearance• We need to improve its appearance

Page 21: 05 access howtolookupselect

Subform property sheet• Set

• Default view to Single Form• Record selectors to No• Navigation buttons to No• Scroll bars to Neither• Delete the label

Page 22: 05 access howtolookupselect

Subform appearance

Page 23: 05 access howtolookupselect

Subform update• The subform updates when you use the

record selectors, but not when you use the list box

• Fix this using some VBA (Visual Basic for Applications)

Page 24: 05 access howtolookupselect

Subform update• Select the list box• Then click on the three dots in the After Update property

List box selected Three dots

Page 25: 05 access howtolookupselect

VBA• A dialogue box opens and you choose Code

Builder

Page 26: 05 access howtolookupselect

VBA

• The VBA code window opensSpace for your code

Form this event

applies to

Event this code

applies to

Page 27: 05 access howtolookupselect

VBA• We want the subform to update after we have

clicked to select a customer.

Refresh the main formRequery

the sub form

Page 28: 05 access howtolookupselect

End result• The data will now match the selection in the

list box