1. Trang chủ
  2. » Công Nghệ Thông Tin

apress pro access 2010 development phần 4 ppt

58 325 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 58
Dung lượng 1,13 MB

Nội dung

CHAPTER 7 ■ CREATING A CHECKOUT FORM 176 10. Drag the CategoryDescription control to the first cell in the fourth row. Drag the MediaDescription control to the second cell. Drag the LoanPeriod control to the fourth cell. Again, its label should move to the third cell. 11. Drag the Description control to the fifth row. Merge all the cells on this fifth row into a single cell. Modifying the Form Design Next you’ll perform some cleanup, removing unused cells and setting an appropriate cell height. 1. Remove all of the empty rows. 2. Select one of the controls in the layout and click the Select Layout button to select all of the controls. Click the Control Padding button, and then click the None link. 3. You may have noticed that the blank rows at the top of the layout are not as high as the other rows. To assure uniformity, select all the cells and in the Format tab of the Property Sheet, set the Height property to .22." The Description control will need to be larger, however. Select only the Description control and resize it to be about 3 lines high. 4. Select the entire layout and drag it to the top-left corner of the form. 5. Go to the Layout View and resize the controls as necessary so the data fits properly. 6. Go back to the Design View and shrink the form size to remove all unused space. Finalizing the Form Details In the final step, you’ll lock down the fields that should not be editable and finalize the form implementation. 1. Select the following controls and in the Data tab of the Property Sheet, set the Locked property to Yes. This will prevent the user from modifying these fields because the only fields that will be editable are Condition and Comment. • Title • Author • Status • CategoryDescription • MediaDescription • LoanPeriod • Description CHAPTER 7 ■ CREATING A CHECKOUT FORM 177 2. Save the form and enter the name InventoryItemLookup when prompted. 3. You will need an unbound control that you can use to indicate if a record was read successful. Add a TextBox control to the form and set its Name property to lblTitle. Also set its Visible property to No. Delete the associated label. 4. In the Property Sheet, select the Form object and the Event tab. For the On Current property select Event Procedure and click the ellipses. Enter the following code for the implementation of this event handler: lblTitle = Title This code simply updates the unbound control with whatever is in the Title field. ■ Tip The code that checks to see if a record was read successfully first sets one of the fields to a blank string. It then re-queries the form and checks to see if the value is still blank. You can’t do this with a bound control, because setting it to blank will update the record. Instead you’ll use the lblTitle control, which is copied from the bound Title control when a record is read. The final layout should look like Figure 7-23. Figure 7-23. The layout of the InventoryItemLookup form 5. Just like the CustomerDisplay form that you designed earlier, this form should display a single record so you’ll need to remove all the navigation controls. In the Property Sheet, select the Form object and the Format tab. Set the following property values: • Border Style: None • Record Selectors: No • Navigation Buttons: No • Scroll Bars: Neither CHAPTER 7 ■ CREATING A CHECKOUT FORM 178 • Control Box: No • Close Button: No • Min Max Buttons: None 6. Also, in the Data tab, set the Filter property as [InventoryItemID] = InventoryItemID and set the Filter On Load property to Yes. Test out the form change switching to the Design View. The form should look like Figure 7-24. Figure 7-24. The completed InventoryItemLookup form Linking the InventoryItemLookup Subform Now you’re ready to add the InventoryItemLookup form as a subform to the CheckOut form. This will be done just like you added the CustomerDisplay form. You’ll add a TextBox control where the user can enter the ID of the inventory item. This will be linked to the child form so the details can be displayed. 1. Close the InventoryItemLookup form and then open the CheckOut form in the Design View. 2. You’ll need to add some more cells to drop the new controls into. Go the Arrange tab of the ribbon. Select the existing layout control and click the Insert Right button five times and the Insert Below button once. 3. Add a TextBox control to the Form Header and set its Name property to txtInventoryItemID. Enter the Caption for the associated label as InventoryID: and set the Text Align property to Right. Drag the txtInventoryItemID control to the top row, leaving two blank cells to the right of the Search button. The label should move to the left of this control, leaving a single blank cell after the Search button. 4. Merge the last four cells of the last two rows into a single cell. This will leave a blank column between the existing customer controls and the new inventory item controls. 5. From the Design tab of the ribbon, click the Subform button and then click inside the Form Header. This will start the Subform Wizard. Select the InventoryItemLookup form, as shown in Figure 7-25. CHAPTER 7 ■ CREATING A CHECKOUT FORM 179 Figure 7-25. Selecting the existing InventoryItemLookup form 6. In the next dialog box, accept the default name, which should be InventoryItemLookup. 7. Delete the associated label that was generated and drag the subform to the merged cell at the bottom right of the Form Header. 8. Switch to the Layout View and resize the cells so the subform fits properly. The layout should look like Figure 7-26. ■ Tip To increase the vertical size of the merged cell without affecting the cells used by the CustomerDisplay form, select the blank cell underneath the CustomerDisplay form and change its height. Likewise, to increase the horizontal size, select the cell to the right of the txtInventoryItemID control and increase its width. Figure 7-26. The layout of the CheckOut Form Header CHAPTER 7 ■ CREATING A CHECKOUT FORM 180 9. Go back to the Design View and select the InventoryItemLookup subform. In the Data tab of the Property Sheet, enter txtInventoryItemID for the Link Master Fields property and InventoryItemID for the Link Child Fields property. This will establish the link between the parent and child forms. Also, in the Format tab, set the Visible property to No. 10. The last step is to implement the LostFocus event for the txtInventoryItemID control. Select this control and in the Event tab of the Property Sheet, select Event Procedure for the On Lost Focus property. Then click the ellipses to display the VBA code. 11. For the txtInventoryItemID_LostFocus method, enter the following implementation: InventoryItemLookup!lblTitle = "" DoCmd.Requery "InventoryItemLookup" If (Len(InventoryItemLookup!lblTitle) > 0) Then InventoryItemLookup.Visible = True Else InventoryItemLookup.Visible = False End If This code should be familiar to you since it’s very similar to the LostFocus event that you implemented for the txtCustomerID control. It clears the unbound TextBox control (lblTitle), re-queries the form and then checks to see if the control is still blank. Save the code and save the form. Test out the CheckOut form by switching to the Form View. Enter an InventoryItemID and verify that it displays the data correctly. Try entering a comment and changing the condition. Also try entering an invalid InventoryItemID and verify that no information is displayed. Designing the CheckOut Details When an item is checked out, a Loan record is created. The Detail section will contain these Loan records; as items are checked out, they will be added to the Detail section of the form. The record source for the form will be the Loan tabled, filtered to only include those records loaned out to the current customer. However, the customer could have items that were previously checked out so the query needs to only retrieve the current items. To accomplish that, you’ll need to modify the Loan table to add an InProgress field. You’ll set the default value to be Yes so all new records will show as in progress. When the checkout has completed, you’ll reset this flag. Altering the Loan Table Open the Loan table in the Design View. Add a new field named InProgress and select Yes/No for the Data Type. In the Field Properties window, set the Default Value property to 1, as shown in Figure 7-27. CHAPTER 7 ■ CREATING A CHECKOUT FORM 181 Figure 7-27. Adding the InProgress field Creating a LoanDetail Query The next step is to create a query that returns all in progress records for the current customer. From the Create tab of the ribbon, click the Query Design button. Add the following tables to the query: • Loan • InventoryItem • Item There are two relationships between the Loan and InventoryItem tables; delete the one between Loan.LoanID and InventoryItem.CurrentLoanID. (This only affects the current query and not the underlying table relationships.) Add the following fields to the query: • Loan.CustomerID • Loan.InventoryItemID • Loan.CheckedOut • Loan.DueDate • Loan.InProgress • Item.Title For the CustomerID field, enter [Forms]![CheckOut]![txtCustomerID] for the criteria. For the InProgress field, enter True for the Criteria. Save the query and enter the name as LoanDetail when prompted. The query design should look like Figure 7-28. CHAPTER 7 ■ CREATING A CHECKOUT FORM 182 Figure 7-28. The query design for LoadDetail Designing the Detail Section Now you’ll set up the new LoanDetail query as the record source for the CheckOut form and add data- bound controls to the Detail section. This will display the items that have been checked out so far. 1. Open the CheckOut form in the Design View. In the Property Sheet, select the Form object and the Data tab. For the Record Source property, select the LoanDetail query. 2. Click the Add Existing Fields button in the ribbon. Double-click each of the following fields to add it to the form: • InventoryItemID • Title • DueDate • CustomerID 3. Select all of these controls. From the Arrange tab of the ribbon, click the Tabular button. The labels will be placed in the Form Header. 4. Select all of the labels and drag them to the bottom of the Form Header. Select all of the data-bound controls and drag then to the top of the Detail section. Select both the controls and the labels and drag them to the left edge of the form. 5. Make the Title and DueDate controls wider. CHAPTER 7 ■ CREATING A CHECKOUT FORM 183 6. The InventoryItemID and CustomerID controls were created as ComboBox controls. These will be read-only fields, so you’ll need to replace them with TextBox controls. Delete both controls. 7. Add a TextBox control to the Detail section. Delete the associated label. Change the Name property to InventoryItemID, and select InventoryItemID as the Control Source. 8. In the same manner add another TextBox control, delete the associated label, set the Name property to CustomerID, and select CustomerID as the Control Source. 9. Drag these controls into the cells where the ComboBox controls were. 10. Delete the label for the CustomerID field. Select the CustomerID control and set the Visible property to No. The CustomerID control is used for inserting records and does not need to be displayed. 11. Select all the data-bound controls in the Detail section and set the Locked property to Yes. 12. In the Property Sheet, select the Form object and set the Default View property to be Continuous Form. Adding the Header and Footer Controls There are just a few more things to finish up the form design. You’ll need a CheckOut button in the Form Header that will create the Loan record. You’ll also need a Complete button in the Form Footer that will execute the final step of the process. You’ll also add a control to the Form Footer to keep a running count of the number of items being checked out. 1. Add a command button to the Form Header. Cancel the Command Button Wizard and drag the control to the top-right cell. Set the Name property to CheckOut and the Caption property to CheckOut as well. Set the Visible property to No. 2. Add a command button to the Form Footer. Again, cancel the wizard and enter the Name and Caption properties as Complete. 3. Add a TextBox control to the Form Footer. Enter the Caption of the associated label as Items checked out: and set the Text Align property to Right. Change the control name to txtCount. Enter =Count(*) for the Control Source property. 4. Select all three fields in the Form Footer. In the Arrange tab of the ribbon, click the Stacked button. Click the Insert Right button twice to add two more columns. 5. Drag the Complete button the right-most cell in the first row and delete the empty row. 6. Drag the layout control to the top-left corner of the Form Footer. Resize the third cell to move the Complete button to the right. CHAPTER 7 ■ CREATING A CHECKOUT FORM 184 The final layout should look like Figure 7-29. Figure 7-29. The layout of the CheckOut form Implementing the CheckOut Logic Now you’re ready to implement the checkout process. At this point, the user can select a customer and an inventory item. That’s all you need to create a Loan record. The first thing you’ll need to do is hide the CheckOut button until both a CustomerID and InventoryItemID have been entered. Then the CheckOut button needs to be implemented so it inserts a record into the Loan table. Finally, you’ll implement the Complete button that will finalize the process. Enabling the CheckOut Button Edit the VBA code for the txtInventoryItemID LostFocus event and add some code to hide or display the CheckOut button. Listing 7-4 shows the modified code and the new lines are shown in bold. Listing 7-4. The modified LostFocus Event InventoryItemLookup!lblTitle = "" DoCmd.Requery "InventoryItemLookup" If (Len(InventoryItemLookup!lblTitle) > 0) Then InventoryItemLookup.Visible = True If (Len(CustomerDisplay!lblName) > 0) Then CheckOut.Visible = True If (InventoryItemLookup!Status = "Available") Then CheckOut.Enabled = True Else CheckOut.Enabled = False End If Else CHAPTER 7 ■ CREATING A CHECKOUT FORM 185 CheckOut.Visible = False End If Else InventoryItemLookup.Visible = False CheckOut.Visible = False End If The new code makes the button visible if there is an inventory item displayed in the InventoryItemLookup subform. The button is then enabled if the item is available. Otherwise, the button is disabled. If the user scans an item that is not available for some reason, they’ll see a grayed CheckOut button that will indicate the item cannot be checked out. The details in the subform such as Status and Comment should explain why it is not available. Implementing the CheckOut Button If the item is available to be loaned out, the user can click the CheckOut button. This will insert a record into the Loan table and perform some clean-up to prepare for additional records to be checked out. Right-click the CheckOut button and click the Build Event link. At the prompt choose Code Editor. Enter the implementation of the CheckOut button using the code from Listing 7-5. ■ Tip The OnClick event is the default event for a command button so you can use the shortcut approach to creating an event handler. For other events, you should select the event in the Property Sheet and click the ellipses like you have done before. Listing 7-5. The Implementation of the CheckOut Button ' Go to a new record DoCmd.GoToRecord acDataForm, "CheckOut", acNewRec CustomerID = txtCustomerID InventoryItemID = txtInventoryItemID ' Cause the focus to move off the current record, which ' will cause it to be saved DoCmd.GoToRecord acDataForm, "CheckOut", acNext DoCmd.GoToRecord acDataForm, "CheckOut", acLast ' Reset the form controls txtInventoryItemID = "" DoCmd.Requery "InventoryItemLookup" InventoryItemLookup.Visible = False CheckOut.Visible = False This code manipulates the form to insert a record rather than executing a SQL command directly. The GoToRecord method allows you to navigate through the records on the form just like the navigation controls at the bottom of the form. The last parameter of this function species the navigation option [...]... read-only so the record selector was unnecessary To remove this, set the Record Selectors property to No 2 Navigation Controls: The navigation controls are at the bottom of the form The Navigation Buttons property determines if this row of controls is visible or not The “1 of 44 3” text lets you know there are 44 3 records currently available and you’re on the first one There are buttons to go to the... the CustomerID control 4 Shrink the CustomerID control to about half of its original height and width In the Format tab of the Property Sheet, set its Visible property to No 5 Right-click the CustomerID control and click the Position ➤ Bring to Front links, as shown in Figure 8 -4 This will keep other controls from hiding it 193 CHAPTER 8 ■ CREATING A CUSTOMER ADMIN FORM Figure 8 -4 Bringing the CustomerID... navigation functions If the Navigation Buttons property is set to No, the areas marked as 3, 4, and 5 are not displayed either 3 Navigation Caption: The word “Record” is defined by the Navigation Caption property If no value is specified, the default text, “Record,” is used If you wanted this to be “Customer,” just enter that text in the Navigation Caption property 4 New Record: The control just to the right... before I added this one.) 2 Click just below the tab to highlight a rectangle around the page The Property Sheet should indicate that the Page11 object is selected (or whatever your page was named) 3 In the Format tab, change the Caption property to Profile In the Other tab, change the Name property to Profile 199 CHAPTER 8 ■ CREATING A CUSTOMER ADMIN FORM ■ Caution When adding a control to a page,... following Name and Caption properties: • ModifyCustomer: Modify… • NewCustomer: New… • Finish: Finish 13 You may need to adjust the height of the row containing the command buttons 14 Select all the controls (the two subforms and the three buttons) and in the Format tab of the Property Sheet, set the Visible property to No You will display these controls with VBA code as appropriate 15 Save the form... filter and navigation properties 1 Select the entire layout, click the Control Padding button in the ribbon, and click the None link While everything is still selected, from the Format tab of the Property Sheet, set the Height property to 22." 2 Select all the labels in the first column and set the Text Align property to Right 3 Drag the entire layout to the top-left corner of the form 4 Remove the controls... high 2 Add a TextBox control to the Form Header and change the Name property to txtCustomerID Set the Caption of its associated label to CustomerID: Set the Text Align property for the label to Right 3 Add a command button to the Form Header and cancel the Command Button Wizard Change the Name property to Search and set the Caption property to Search… ■ Tip You can disable the wizards, such as the Command... some of label captions (these can be edited directly on the form): • • State Province: State • Zip Postal: Postal • 9 Country Region: Country PhoneNumber: Phone Select all the cells in the layout and, in the Format tab of the Property Sheet, set the Height property to 22." 10 Select all the label controls and set their Text Align property to Right 11 Drag the entire layout control to the top-left corner... experience Designing the Profile Page The Profile page will contain the CustomerDisplay form that you implemented in Chapter 7 and the CustomerUpdate form that you just created You will also need a few command buttons to tie everything together 1 Make sure the control wizards are enabled and the Profile page is selected Click the Subform button in the ribbon, and then click inside the Profile page In the... when prompted Open the form in Form View, which should look like Figure 8-6 Figure 8-6 Completed design of the CustomerUpdate form Configuring the Form Controls In Chapter 7, I gave some instructions for setting various form properties without explaining what they were for I’ll now explain the form controls that Access generates for you, how they work, and how to configure them using the form Property . Also set its Visible property to No. Delete the associated label. 4. In the Property Sheet, select the Form object and the Event tab. For the On Current property select Event Procedure and click. Property Sheet, set the Height property to .22." The Description control will need to be larger, however. Select only the Description control and resize it to be about 3 lines high. 4. . Filter property as [InventoryItemID] = InventoryItemID and set the Filter On Load property to Yes. Test out the form change switching to the Design View. The form should look like Figure 7- 24.

Ngày đăng: 12/08/2014, 15:22

TỪ KHÓA LIÊN QUAN