CHAPTER 3
Using Forms to Collect, Filter, and Display Information
Forms that work well with each other make your application easy to use. This chapter presents a variety of techniques you can use to automate your forms and to make individual forms work together as a unified application.
Customizing a Command Button Created with a Wizard
Using the Command Button Wizard is a great way to create most general-purpose command buttons on your forms. However, a command button created with the wizard may not do exactly what you want it to do. Rather than creating a command button from scratch, you can use the Command Button Wizard to create the button and an event procedure for its Click event, and then customize the way the button works by editing the event procedure. For example, you can use the Command Button Wizard to create a button that opens a form, and then you can edit the event procedure so that it opens the form for read-only access instead of for editing data.
The event procedure that the Command Button Wizard creates includes simple Visual Basic statements that perform the action you specify. In addition, the wizard adds a simple method of error handling to respond appropriately to run-time errors. If an error occurs, the error-handling code displays the Microsoft Access error message and then exits the event procedure. The following illustration shows the event procedure the Command Button Wizard creates for a command button that opens a form.
To change or enhance the way the button works, you edit the lines of code that are highlighted in the previous illustration. Unless you want to change the way the event procedure handles errors, you don't need to edit any other lines in the procedure.
See Also For more information on error handling in Visual Basic, see Chapter 8, "Handling Run-Time Errors."
Customizing a Command Button That Opens a Form
Customers sometimes ask Northwind sales representatives for further information about a product on their order. You can provide a command button on the Orders form to open a pop-up form that shows details about the product selected in the Orders subform.
Step One: Create the button that opens the form The Orders sample application includes a form named ProductsPopup that displays details about Northwind products. You can open the Orders form in Design view and use the Command Button Wizard to add a command button named Details that opens the ProductsPopup form. The Command Button Wizard creates the command button and writes an event procedure for the command button's Click event.
However, the pop-up form displays details about a value on the subform, not the main form. Because the wizard can't filter based on a subform value, you'll need to modify the command button's event procedure yourself so the pop-up form shows information from the correct product.
See Also For more information on creating the ProductsPopup form, see "Using Pop-up Forms and Dialog Boxes" later in this chapter.
Step Two: Open the event procedure In Design view, right-click the command button and then click Build Event on the shortcut menu. Microsoft Access opens the form module and displays the button's Click event procedure.
Step Three: Change the code so it filters records You want to filter the records in the ProductsPopup form to show the record whose ProductID is equal to the ProductID of the current record in the Orders subform. To do this, you need to apply a filter to the form when you open it.
The Command Button Wizard automatically creates a variable in the Click event procedure that makes it easy for you to set a filter for the form you're opening. The following line of code sets the strLinkCriteria variable to the appropriate filter string.
strLinkCriteria = "ProductID = Forms!Orders!OrdersSubform!ProductID"
Add this line of code to the Click event procedure that opens the form, as shown in the following example:
Dim strDocName As String
Dim strLinkCriteria As String
strDocName = "ProductsPopup"
strLinkCriteria = "ProductID = Forms!Orders!OrdersSubform!ProductID"
DoCmd.OpenForm strDocName, , , strLinkCriteria
This event procedure passes the filter string you added (strLinkCriteria) as the wherecondition argument of the OpenForm method that opens the form. Microsoft Access then automatically sets a filter on the ProductsPopup form and makes the form display the correct record.
Compile your edited code and test it to make sure it works as expected. Now when you click the command button on the Orders form, the ProductsPopup form opens, showing values in the record for the current product in the Orders subform.
Note When the Details_Click event procedure opens the ProductsPopup form, the ProductsPopup form becomes the active form. In addition to the code that opens the ProductsPopup form, the Details_Click event procedure contains code that moves the focus from the pop-up form to the Orders form, so that sales representatives can continue filling out the order without having to click the Orders form. This code uses the SetFocus method, first to set the focus to the Orders form, and then to set the focus to the Orders subform rather than to the Details command button (the last control on the form to have the focus). To see this code, open the Orders form in Design view. Right-click the Details command button, and then click Build Event on the shortcut menu.
© 1996 Microsoft Corporation. All rights reserved.