tutorial 4: enhancing forms

18
1 Tutorial 4: Enhancing Forms This tutorial covers several usability, data integrity, and control issues that arise in a DBMS-based application. Once again, you will work with the Infinity Computers order entry system from the earlier tutorials. Building User Friendly Drop-down Lists & Combo Boxes Ease of use is a critical feature in any system. Minimizing the amount of typing that the user must do significantly improves ease of use. This helps to reduce dramatically the number of errors that the user makes, which improves data quality. One of the easiest ways to reduce typing is by using drop-down lists and combo boxes on data entry forms. Open OrdEnt5.mdb. Select the Forms tab. Open New Orders. Go to order 13 (Figure 1). The customer and representative fields display numbers, not the names. This is not very user friendly unless the customers and representatives happen to have names that are numbers. J This can happen if the person defining tables does not use the lookup wizard when defining fields storing foreign keys. Fortunately, there is an easy way to fix this shortcoming. Figure 1: New Orders Form Switch to Design View (Figure 2). Select the CustID text box (not the label). Display the properties for this field. Select the All tab. Since this field is currently a text box, we cannot configure it to display a list. However, we can change the field to a combo box.

Upload: others

Post on 21-Feb-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tutorial 4: Enhancing Forms

1

Tutorial 4: Enhancing Forms This tutorial covers several usability, data integrity, and control issues that arise in a DBMS-based application. Once again, you will work with the Infinity Computers order entry system from the earlier tutorials. Building User Friendly Drop-down Lists & Combo Boxes Ease of use is a critical feature in any system. Minimizing the amount of typing that the user must do significantly improves ease of use. This helps to reduce dramatically the number of errors that the user makes, which improves data quality. One of the easiest ways to reduce typing is by using drop-down lists and combo boxes on data entry forms. Open OrdEnt5.mdb. Select the Forms tab. Open New Orders. Go to order 13 (Figure 1). The customer and representative fields display numbers, not the names. This is not very user friendly unless the customers and representatives happen to have names that are numbers. ☺ This can happen if the person defining tables does not use the lookup wizard when defining fields storing foreign keys. Fortunately, there is an easy way to fix this shortcoming.

Figure 1: New Orders Form

Switch to Design View (Figure 2). Select the CustID text box (not the label). Display the properties for this field. Select the All tab. Since this field is currently a text box, we cannot configure it to display a list. However, we can change the field to a combo box.

Page 2: Tutorial 4: Enhancing Forms

2

Figure 2: Form in Design View

Hover over the CustID text box and right click the mouse. In the popup menu select Change To and then select the Combo Box option (Figure 3).

Figure 3: Change a Text Box to a Combo Box

This changes the text box to a combo box, which also changes the properties available for the control (Figure 4). We can now set properties for the combo box so that we get a drop-down list of customers, including the customer id, name, and city.

Page 3: Tutorial 4: Enhancing Forms

3

Figure 4: CustID Combo Box Properties

Click on the text box to the right of the Row Source property. Click the Build button displayed on the far right side of the Row Source text box. This brings up the QBE grid where you can define the row source for the field (Figure 5). (Refer to “Introduction to Microsoft Access Tables, Forms, and Menus” for an overview of the buttons frequently used to update forms.)

Figure 5: QBE Grid

Add the Customers table to the query. Add the customer id, name, and city to the QBE grid (Figure 6).

Page 4: Tutorial 4: Enhancing Forms

4

Figure 6: Query for CustID Row Source

Close the query and save the changes. Notice that the Row Source property now contains an SQL query (Figure 7). What output would this SQL query generate?

Figure 7: Revised CustID Row Source Property

Unfortunately, Microsoft Access is not “smart” enough to make the other changes needed when we change the row source. We have to make the changes manually. Figure 8 shows the updated properties. We will look at each one separately.

Page 5: Tutorial 4: Enhancing Forms

5

Figure 8: Updated Properties

The Column Count property must match the number of fields in the SELECT clause of the query. In our case, we have three fields (id, name, and city). The Column Widths property defines the display widths allocated for the fields in the SELECT clause. The property defines the widths for all columns starting with the left-most column. Use a semicolon (;) to separate the widths for the various columns. In our case, we allocated zero inches for the customer id column, three inches for the name column, and two inches for the city column. Use a column width of zero to hide a column. Typically, we hide the “bound” column, which we also generally specify as the first column in the query. The Bound Column property identifies the field in the SELECT clause whose value gets stored in the field on the form (as defined by the Control Source property). Use the primary key for the table identified in the FROM clause as the bound column. Why use the primary key for the bound column? Display the updated form (Figure 9). Notice that the customer’s name appears in the customer field.

Page 6: Tutorial 4: Enhancing Forms

6

Figure 9: Updated New Orders Form

Update the form to display the sales representative’s name in the representative field (Figure 10). Include any fields from the SalesReps table that you feel are appropriate. You must include at least one field in addition to the sales representative’s name. Which field is that?

Figure 10: New Orders Form Using Lists

Events and Event Handlers In some cases, you may want to tie the value of one field to the value of another field. For example, say you have an event scheduled for a particular date and you want to confirm the event prior to the actual scheduled date. You can use an event handler to set the confirmation date.

Page 7: Tutorial 4: Enhancing Forms

7

Close any forms you currently have open. Save changes if prompted. From a computer’s standpoint, each time the user clicks a button, enters a field, exits a field, changes a value, or selects an item from a list, an event occurs. Microsoft Access provides the capability for you to define Visual Basic for Applications (VBA) program code, called an event handler, to respond to specific types of events. VBA is a programming language based on the BASIC (Beginners All-purpose Symbolic Instruction Code) programming language. A number of Microsoft and non-Microsoft applications, including many major accounting systems, integrate VBA support for customizing data entry forms and reports. An event handler is program code executed in response to an event. The following are some of the most common events.

Figure 11: Common Events

Event Triggered … Typical Scope On Current before Access displays a new record on a form. Form Before Insert before Access adds a new record to a table. Form After Insert after Access adds a new record to a table. Form Before Update before Access changes a value in a table. Field, Form After Update after Access changes a value in a table. Field, Form On Delete before Access deletes a record in a table. Form On Change after the contents change. Text Box, Combo Box On Undo before Access undoes a change. Field Avoid the On Change event when working with text boxes since the event handler executes each time the user presses a key that modifies the field. Instead, use the After Update or Before Update events with text boxes. Open the Date Example form in Design View. Select the EventDate text box. Display the properties for this field. Select the Event tab (Figure 12).

Figure 12: Event Handlers

In this case, we want the confirmation date to be one week (seven days) before the event date. When the user sets the event date, Microsoft Access should set the confirmation date automatically. Microsoft

Page 8: Tutorial 4: Enhancing Forms

8

Access uses text boxes to edit date values. Use the After Update event and event handler to set the confirmation date based on the event date. Select the After Update event by clicking on the text box next to After Update in the event list. Click the Build button. Select Code Builder and click OK. This displays the VBA editor with the AfterUpdate event handler for the EventDate field selected. Notice that Access automatically generates the opening and closing code for the event handler.

Figure 13: VBA Editor

Type the line of code shown in Figure 14.

Figure 14: Set Confirmation Date Code

This code sets the value of the ConfirmDate field to the value of the EventDate field minus seven days. Although the square brackets around the field names are not required in this example, including the

Page 9: Tutorial 4: Enhancing Forms

9

square brackets is a good practice to avoid problems. When are square brackets required around field names? Select File and then Close and Return to Microsoft Access. Display the form and enter a valid date in the Event Date field. Once you move off the field, Microsoft Access should automatically set the value of the Confirm Date field to seven days prior to the event date.

Figure 15: Example of Dates Tied Together

Tracking Historical Prices One issue to address during database design and development is how to handle price changes over time. Businesses address this issue in a number of ways. Perhaps the easiest way to do this is by storing the price in effect at the time of the transaction with the transaction detail. You must make four changes to your database. 1. Define a SalesPrice field in the OrderItems table. 2. Update any existing records to contain the current sales price. (You only need to do this step is

transaction data already exists in the database. Perform this step once.) 3. Modify the order entry form to use the SalesPrice field in the OrderItems table instead of the Price

field in the Products table. 4. Modify the order entry form to transfer the value of the Price in the Products table to the SalesPrice

field in the OrderItems table each time the user enters a new order item. Close any forms you currently have open. Save changes if prompted. Define a SalesPrice Field Open the OrderItems table in Design View. Add a field after the Quantity field, name the field SalesPrice, and set the data type to Currency (Figure 16). Save the updated table. The SalesPrice field stores the price in effect at the time of a sale. This way if the price changes, we can still produce reports that contain the correct price.

Your event id may not match this example.

Page 10: Tutorial 4: Enhancing Forms

10

Figure 16: SalesPrice Field in OrderItems Table

Set the SalesPrice Value in OrderItems for Existing Records Open the OrderItems table. The table contains a number of records, but the SalesPrice field is blank since you just created the field.

Figure 17: OrderItems Table with SalesPrice Field

Normally you would have created the SalesPrice field before you added any data to the database so this would not be a problem. Since there are already transaction records in the database, we must assign a value to the SalesPrice field for each record currently in the table. To resolve this problem, go the Queries tab and run the query named Set OrderItems Sales Price Field. As long as you defined the SalesPrice field as specified above, this query will set the sales price for each record to the correct value. Click Yes in response to the “You are about to run an update query that will modify data in your table” message. Click yes in response to the “You are about to update 38 row(s)” message.

Page 11: Tutorial 4: Enhancing Forms

11

Update the Order Entry Form to Use the SalesPrice Field Open the SalesPrice Orders form in Design View. Activate the subform’s Form Selector. Display the Record Source for the subform (Figure 18).

Figure 18: Subform Record Source

The record source currently includes the Price field from Products. Delete this field from the QBE grid, add the SalesPrice field from OrderItems, and update the formula for the extended price (Ext Price) to use the SalesPrice field rather than the Price field (Figure 19). Finally, remove the Products table from the table panel since you no longer use this table in the query.

Figure 19: Updated Record Source

Close the query builder and save changes. Display the properties for the Price text box in the subform. Select the All tab. Change the Name property to SalesPrice. Pull down the list of control sources and select SalesPrice for the Control Source property (Figure 20).

Subform’s Form Selector activated

Page 12: Tutorial 4: Enhancing Forms

12

Figure 20: Use the SalesPrice as the Control Source

Save the form. Display the form. Notice that the price for a Pentium 4 2GHz Desktop is $1,500. Close the form. Open the Products table and change the price of a Pentium 4 2GHz Desktop to $150,000. Close the table. Open the New Orders form. Notice that the price for the Pentium 4 2GHz Desktop is $150,000. Close the form. Open the SalesPrice Orders form. Is the price of a Pentium 4 2GHz Desktop better with this form? Why? Close the form. Open the Products table and change the price of a Pentium 4 2GHz Desktop back to $1,500. Close the table. Update the Order Entry Form to Set the SalesPrice Field Open the SalesPrice Orders form and add a new order as shown in Figure 21. Notice how the sales price does not get set when you add the product ordered. This is because we have not updated the form to set the sales price when the user enters a sale. This is a major problem!

Page 13: Tutorial 4: Enhancing Forms

13

Figure 21: Order without a Sales Price

Switch to Design View. Display all properties for the ProductID combo box in the subform. Update the combo box to include the price of the product as the third field (Figure 22).

Figure 22: Include the Price in the Products List

, Products.Price

Make changes as needed.

Page 14: Tutorial 4: Enhancing Forms

14

Now we need to create some VBA code to save the product’s price in the SalesPrice field when the user selects a product from the list. After the user selects a product from the drop-down list, the form should copy the product’s current price to the SalesPrice field for the order item. Use the After Update event to make the copy. The current price is the third column in the ProductID combo box. Figure 23 shows the VBA code. The one quirk you have to deal with is that VBA numbers columns different from what one might expect. From VBA’s standpoint, the first column in the recordset resulting from the Row Source SQL query is column zero, the second column is column one, and so on. As a result, the VBA code retrieves the price from column two, which is really the third field in the SELECT clause.

Figure 23: After Update Event Handler to Set Sales Price

Close the VBA editor. Close the form and save any changes. Open the form and test it by entering a new order from customer O. McDonald, handled by sales representative Charles E. Chees, for one 128MB Memory Expansion – Notebook. Verify that the price, $64.95, displays properly. Default Dates When capturing data about transactions, you may want the transaction date to default to the current date. This is easy to do. Close any forms you currently have open. Save changes if prompted. Open the SalesPrice Orders form in Design View. Select the text box for the OrderDate field. Display the properties for this field. Select the All tab. Enter =Date() for the Default Value property. This tells Microsoft Access to use the current date for the default value.

[SalesPrice] = [ProductID].Column(2)

Page 15: Tutorial 4: Enhancing Forms

15

Figure 24: Default Date

The default value applies to new records, so specifying a default date does not change records already in the database. Controlling the User’s Access to Fields In many cases, you may not want the user of a form to be able to change the value of a field. Microsoft Access provides properties to address these situations. Close any forms you currently have open. Save changes if prompted. Open the Protected Orders form. Change the discount rate for order 1 to 0.15 and tab to the next field. Notice how this change has a big impact on the order total. Would you want users to be able to make this kind of change? Press the Escape (Esc) key to undo this change. Switch to Design View. Select the DiscountRate text box. Display the properties for this field. Select the Data tab.

Page 16: Tutorial 4: Enhancing Forms

16

Figure 25: Enabled and Locked Properties

Look at the current values for the Enabled and Locked properties. The Enabled property determines if the user can access (place the cursor on) a field. If set to Yes, the user can access the field; otherwise, the user cannot access the field. The Locked property determines if the user can change the value of a field. If set to Yes, the user canno t change the value of the field; otherwise, the user can change the value of the field. At first glance, it would seem that these properties are redundant; however, there is a reason why Microsoft Access provides both properties. The following table summarizes the effect of each possible combination of values for these properties.

Figure 26: Enabled and Locked Properties

Enabled Locked Effect Yes No The user may edit or copy the data.

Use if you want the user to be able to edit the field value. Yes Yes The user may copy but not edit the data.

Use if you want the user to be able to copy but not edit the field value. No No The user may not edit or copy the data. The control displays dimmed.

Do not use. No Yes The user may not edit or copy the data.

Use if you do not want the user to be able edit or copy the field value. Set the Enabled and Locked properties to the fourth combination (Enabled = No, Locked = Yes). Display the form. Attempt to click on the discount rate field. That does not work! Switch back to Design View. Change the Locked property to No. Display the form. Once again, you cannot access the discount rate field. In addition, the field displays dimmed. Many people do not like this dimmed appearance. Switch back to Design View. Change the Locked property back to Yes. Save the form.

Page 17: Tutorial 4: Enhancing Forms

17

Setting the Tab Order The tab order is the order in which the cursor moves from field to field when the user presses the Tab key. Ideally, the cursor starts at the first editable field at the top, left corner of the form. As the user presses the Tab key, the cursor typically moves left to right and top to bottom from one editable field to the next. As you edit a form, you are likely to move fields. As a result, you are likely to disrupt the default tab order for the form. Close any forms you currently have open. Save changes if prompted. Open the Protected Orders form. The cursor should start on the order date field. Press Tab once. The cursor skips the customer and sales representative fields and moves to the product id field in the subform. This is because I messed up the tab order when I built the form. Switch to Design View. Select View (on the menu) and Tab Order … (Figure 27).

Figure 27: Display Tab Order

Page 18: Tutorial 4: Enhancing Forms

18

Use the Tab Order dialog box to change the tab order (Figure 28).

Figure 28: Tab Order Dialog

The Auto Order option resets the tab order to left-to-right, top-to-bottom (Figure 29). In many cases, this is the best setting. Use Auto Order.

Figure 29: Automatic Tab Order

Display the form. Verify that the tab order is correct. If you want to set the tab order manually, follow the instructions displayed in the Tab Order dialog box.