[ Pobierz całość w formacie PDF ]
.OpenReport Customer Sale Letter ,ReportDest, , [Customer]![Last Visit Date] Between me![FromDate] and me![To Date]End If Print EnvelopeIf Me![Envelope] = -1 ThenDoCmd.OpenReport Customer Envelope ,ReportDest, , [Customer]![Last Visit Date] Between me![FromDate] and me![To Date]End If Print Mailing LabelIf Me![Mailing Label] = -1 ThenDoCmd.OpenReport Customer Mailing Labels ,ReportDest, , [Customer]![Last Visit Date] Between me![FromDate] and me![To Date]3596-X ch31.F 5/30/01 11:47 AM Page 1027Chapter 31 &' Using Visual Basic in Forms and Reports1027End IfCase 3 Customer Type Print Sale LetterIf Me![Sale Letter] = -1 ThenDoCmd.OpenReport Customer Sale Letter ,ReportDest, , [Customer]![Type of Customer]=me![CustomerType]End If Print EnvelopeIf Me![Envelope] = -1 ThenDoCmd.OpenReport Customer Envelope ,ReportDest, , [Customer]![Type of Customer]=me![CustomerType]End If Print Mailing LabelIf Me![Mailing Label] = -1 ThenDoCmd.OpenReport Customer Mailing Labels ,ReportDest, , [Customer]![Type of Customer]=me![CustomerType]End IfEnd SelectEnd SubWorking with Combo-Box andList-Box ControlsYou have seen in previous examples how combo boxes and list boxes provide aneffective method of validating data entry in a form.These boxes can display a list ofvalues that the user can choose so that the user does not have to memorize cus-tomer numbers, for example, or remember the spelling of a customer s name.Figure 31-27 shows the Select a Pet combo box for the Add a New Visit Entry form.In the Mountain Animal Hospital database, the name of the form is Adding Visits forNew Pets.3596-X ch31.F 5/30/01 11:47 AM Page 1028Part V &' Access Application Tools1028Figure 31-27: Using a combo box to validate data entryHandling a combo box entry that is not in the listAs an option, you can set up a combo box to accept a value that is not in the list.Suppose that a customer brings in a new pet for a visit.The Select a Pet combo boxin the New Visit Entry form displays only pets that have visited Mountain AnimalHospital before.Instead of making the user exit the New Visit Entry form and enterthe new-patient pet in the Pets form, you can allow the user to type the name of thepet in the combo box.When the user types in the combo box a value that is not inthe list of values, the Pets Data Entry form automatically appears.When the usercompletes the information for the pet and closes the Pets Data Entry form, the NewVisit Entry form appears, and the combo box displays the pet in the list.Entering in the combo box an item that is not in the list of valid values triggers theNotInList event.Figures 31-28 and 31-29 show the properties for the Select a Petcombo box.By connecting an event procedure to the NotInList event, you can over-ride the normal Not in list error that is built into Access and allow the user to addthe new value to the list.Notice that you also must set the LimitToList propertyto Yes.3596-X ch31.F 5/30/01 11:47 AM Page 1029Chapter 31 &' Using Visual Basic in Forms and Reports1029Figure 31-28: Limiting combo box values to items in the listFigure 31-29: Handling combo box values that are not in the listThe LimitToList property determines how Access responds to entries that donot match any of the list items.When you set LimitToList to No, Access acceptsanything that the user enters, as long as the entry conforms to the ValidationRuleproperty, if one exists.When you set LimitToList to Yes and the user enters aninvalid value, Access checks to see whether an event procedure exists for theNotInList event property.If no procedure is attached, Access displays the standardItem Not In List error message; otherwise, Access does not display the error mes-sage and runs the procedure instead.Figure 31-30 shows the NotInList procedure,called Visits_Pet_ID_NotInList, for the Select a Pet combo box.3596-X ch31.F 5/30/01 11:47 AM Page 1030Part V &' Access Application Tools1030Figure 31-30: A procedure for combo box values that are not in the listCautionMake sure that you change the Limit To List property in the combo box to Nobefore using the On Not in List event.The Const declarations at the top of the Visits_Pet_ID_NotInList procedure arecalled symbolic constants.Constants are variables whose values do not changeduring execution of the procedure, and symbolic constants are names for certainconstants.Symbolic constants are names for certain values that are available anywhere inyour Visual Basic procedures.The actual values may be 0, 1, or 2, but providing ameaningful name makes it easier to understand what your code is doing.Using all-uppercase characters to name symbolic constants helps to differentiate them fromvariables.Constants that are built into Access are upper- and lowercase and beginwith the letters ac.The constants used in this procedure are used as the arguments for the MsgBoxfunction.Instead of using numbers such as 52 and 0 as the arguments, you can cre-ate a meaningful name that makes it easier to understand what your code is doing.This procedure first displays a confirmation message to make sure that the userreally wants to add the new item.If the user chooses No (they do not want to addthe item), the procedure ends.The user then must choose a valid item from the list.3596-X ch31.F 5/30/01 11:47 AM Page 1031Chapter 31 &' Using Visual Basic in Forms and Reports1031Before exiting a NotInList procedure, you must set the Response variable to one ofthree values.The Response variable tells Access what to do with the invalid item.The three values for the Response variable are represented by symbolic constants.These symbolic constants are built into Access and are available to any of yourVisual Basic programs.Table 31-4 describes the three Response values.Table 31-4Values for the Response VariableValue DescriptionacDataErrDisplay Displays the standard error message and does not add the itemto the listacDataErrContinue Does not display the standard error message and does not addthe item to the listacDataErrAdded Does not display the standard error message and reruns thequery for the RecordSourceTipIf you use acDataErrContinue or acDataErrAdded as Response values, you need tomake sure that you display a message to the user at some point in your procedure.Otherwise, the user will not be able to leave the field and will not know why.If the user does want to add the new item to the combo box, the Visits_Pet_ID_NotInList procedure displays a blank Pets Data Entry form.When the user closesthe Pets Data Entry form, Visits_Pet_ID_NotInList sets the value for the Responsevariable before exiting.Setting Response to acDataErrAdded tells Access to querythe list of items for the combo box again.When Access re-queries the Select a Petcombo box list, it retrieves the newly added pet from the Pets table
[ Pobierz całość w formacie PDF ]