Topic: Working With Forms: MS Access LAB 6
Topic: Working With Forms: MS Access LAB 6
Creating Multiple-Table or Linked Forms Calculating Totals and Using Expressions Creating Pop-Up Forms, Custom Dialog Boxes, and Message Boxes Creating Switch Board Forms
Subforms
A subform is a form within a form. The primary form is called the main form, and the form within the form is called the subform. A form/subform combination is often referred to as a hierarchical form, a master/detail form, or a parent/child form. Subforms are especially effective when you want to show data from tables or queries with a one-to-many relationship. For example, you could create a form with a subform to show data from a Categories table and a Products table. The data in the Categories table is the "one" side of the relationship. The data in the Products table is the "many" side of the relationship-each category can have more than one product.
Fig. 5: Example of Form with a Subform The main form and subform in this type of form are linked so that the subform displays only records that are related to the current record in the main form. For example, when the main form displays the Beverages category, the subform displays only the Products in the Beverages category. When you use a form with a subform to enter new records, Microsoft Access saves the current record in the main form when you enter the subform. This ensures that the records in the "many" table will have a record in the "one" table to relate to. It also automatically saves each record as you add it to the subform.
2. In the New Form dialog box, double-click Form Wizard in the list.
Fig. 6: New Form dialog box 3. In the first wizard dialog box, select a table or query from the list. For example, to create a Categories form that displays Products for each category in a subform, select the Categories table (the "one" side of the one-to-many relationship). 4. Double-click the fields you want to include from this table or query. 5. In the same wizard dialog box, select another table or query from the list. Using the same example, select the Products table (the "many" side of the one-to-many relationship in the Categories form example). Note It doesn't matter which table or query you choose first. 6. Double-click the fields you want to include from this table or query. 7. When you click Next, if you set up the relationships correctly before starting the wizard, the wizard asks which table or query you want to view by. Using the same example, to create the Categories form, click By Categories. 8. In the same wizard dialog box, select the Form With Subform(s) option. 9. Follow the directions in the remaining wizard dialog boxes. When you click Finish, Microsoft Access creates two forms, one for the main form and subform control, and one for the subform.
Explanation for Orders Example: The Orders form is a main form with one subform. The main form, Orders, displays order information, which you can add, edit, or delete. It is bound to Orders Qry query, a hidden query. The Orders subform displays information about the products ordered, which you can also add, edit, or delete. It is bound to the Order Details Extended query. Orders form: The Orders form uses expressions in the following controls: The Subtotal text box uses an expression in the ControlSource property to refer to the value of the OrderSubtotal text box on the Orders subform. The Total text box uses an expression to calculate the total for the order. The OrderDate text box uses Date function in the DefaultValue property to automatically fill in today's date.
The form uses the following settings in form and control properties: The form's Cycle property is set to Current Record so that the focus stays on the current record in the form rather than automatically advancing to the next record. You can use the navigation buttons to switch to a different record.
The HyperlinkAddress property of the DisplayProducts command button is set to Products.doc, a Microsoft Word document. The document opens in Word when you click the command button. The ControlTipText property the CustomerID combo box specifies a tip that is displayed for the control in Form view when the mouse pointer rests on the combo box.
The form uses event procedures in the following properties: The event procedure in the BeforeUpdate property of the CustomerID combo box displays a message if you leave the combo box without adding a value. The event procedure in the AfterUpdate property of the CustomerID combo box sets the values of the controls in the Ship To area of the form to the values of their corresponding controls in the Bill To area of the form. The event procedure in the OnClick property of the PrintInvoice command button prints the Invoice report for the current record. (specify the select query as the filtername argument of the OpenReport method.)
Orders Subform 1. The OrderSubtotal text box uses the Sum function in an expression to calculate the subtotal for the order. The form uses event procedures in the following properties: 2. The event procedure in the OnError property of the form displays a message and undoes any records entered in the subform if the BillTo combo box on the Orders form doesn't have a value. 3. The event procedure in the AfterUpdate property of the ProductID combo box fills in the UnitPrice value for the selected product. 4. Event procedures in the BeforeDelConfirm and BeforeUpdate properties of the form and in the BeforeUpdate property of the ProductID combo box display a message and undo changes if you open the subform as a standalone form and attempt to add, change, or delete records.
Tool Box
Note: A text box is the most common type of control to use to display a calculated value, but you can use any control that has a ControlSource property. 3. On the form or report, click where you want to place the control. 4. Do one of the following: If the control is a text box, you can type the expression directly in the control.
If the control isn't a text box, or if the control is a text box but you want to use the Expression Builder to create the expression, make sure the control is selected3, click Properties on the toolbar to open the control's property sheet, and then type the expression in the ControlSource property box or click the Build button to open the Expression Builder.
Examples of performing arithmetic operations in forms and reports If you use this expression =[Subtotal]+[Freight] Microsoft Access displays The sum of the values of the Subtotal and Freight controls. =[RequiredDate]-[ShippedDate] The difference between the values of the RequiredDate and ShippedDate controls. =[Price]*1.06 The product of the value of the Price control and 1.06 (adds 6 percent to the Price value). =[Quantity]*[Price] The product of the values of the Quantity and Price controls. =[EmployeeTotal]/[CountryTotal] The quotient of the values of the EmployeeTotal and CountryTotal controls.
control
A graphical object, such as a text box , a check box , a command button , or a rectangle , that you place on a form or report in Design view to display data, perform an action, or make the form or report easier to read.
3
form selector The box where the rulers meet in form Design view. Click this box to select the form. Double-click this box to open the form's property sheet.
event procedure: A procedure automatically executed in response to an event initiated by the user or program code, or triggered by the system.