A
wizard consists of a series of dialog boxes that provide a step-by-step interface for creating a database object. The wizard shields users from the complexities of the process. You probably are familiar with wizards such as the Form Wizard, the Report Wizard, and the Database Wizard. Access 11 supports the development of several types of custom wizards:
Table wizards
Query wizards
Form wizards
Report wizards
Data Access Page wizards
Property wizards
Control wizards
Wizard design guidelines are almost identical to builder design guidelines. The main difference is that a wizard generally presents the user with multiple modal dialog boxes, whereas a builder generally consists of a single modal dialog box. The user must meet all the data requirements for the wizard before she can close the last dialog box.
Creating a wizard is more complex than creating a builder. A wizard generally requires a multipage form and code that creates database objects. Consider a wizard that creates a simple form. The wizard comprises two modal dialog boxes, shown in Figures 25.12 and 25.13. The first dialog box asks the user for a form caption, form name, and message to appear on the new form. The second dialog box enables the user to add OK and Cancel buttons to the form. The multipage form and all the code that enables it to work are in the CHAP25LIB.MDA database on the accompanying CD-ROM. |
Each page of the wizard contains code to ensure that it operates successfully. I have called the form frmGetFormInfo. The first page of this multipage form gives the user the opportunity to choose the next action: Cancel, Next, or Finish. The code for the Cancel button looks like this:
Private Sub cmdCancel1_Click() DoCmd.Close End Sub
This code closes the wizard form. The code takes no other actions because the user is canceling the process. If the user clicks Next, this code executes:
Private Sub cmdNext1_Click() DoCmd.GoToPage 2 Me.Caption = "My Form Wizard - Step 2" End Sub
This code moves to the second page of the form and changes the caption of the form to indicate that the user is on step 2 of the wizard. The code under the Finish button looks like this:
Private Sub cmdFinish1_Click() If CreateCustomForm() Then MsgBox "Form Created Successfully" DoCmd.Close Else MsgBox "Unable to Create Form" End If End Sub
This code calls a function called CreateCustomForm, which is responsible for building the actual form. Later in this section I discuss the details of the CreateCustomForm function. If the function returns True, the code closes the wizard form, and displays a message indicating that the process was successful. Otherwise, the code displays a message indicating that it did not successfully create the form, and the user remains in the wizard. The second page of the form contains similar subroutines. The code under the Back button looks like this:
Private Sub cmdBack2_Click() DoCmd.GoToPage 1 Me.Caption = "My Form Wizard - Step 1" End Sub
This code moves back to the first page of the form. If the user chooses Cancel, this code executes:
Private Sub cmdCancel2_Click() DoCmd.Close End Sub
This code closes the form, taking no further action. If the user clicks Finish, the Click event code of the cmdFinish2 command button executes:
Private Sub cmdFinish2_Click() Call cmdFinish1_Click End Sub
This code calls the code under the Click event of the cmdFinish1 command button.
The CreateCustomForm function (located in the basWizards module of the library database), as seen in Listing 25.3, contains the code that actually builds the new form.
The code first creates both form and control object variables. It sets the form object variable to the return value from the CreateForm function. The CreateForm function creates a new form object. The code sets several properties of the new form object: Caption, RecordSelectors, NavigationButtons, and AutoCenter. Next, the function uses the CreateControl function to create a new label. It calls a reference to the new label ctlNew. The code sets the Caption, Width, Height, Top, and Left properties of the new label. If the user indicated that he wanted an OK button, the code creates a new command button. The code sets the Caption, Width, Height, Top, Left, Name, and Properties property for the button. The code uses the InsertText method to insert code for the Click event of the command button. If the user requested a Cancel button, the code sets the same properties. Finally, if the user indicated a name for the new form, the code uses the Save method to save the new form object.Chapter 14, "What Are ActiveX Data Objects and Data Access Objects and Why Are They Important?", covers ADO code.
Like a builder, you need to add a wizard to the Windows registry before you can use it. You can do this by modifying the registry directly or by adding entries to the USysRegInfo table. Figure 25.14 shows the completed registry entry for the custom Form Wizard.
Notice that the function name is MyCustomForm. This is the entry point to the wizard. The Library key designates the name of the library add-in database containing the entry point function. The Description key specifies what appears in the New Object dialog box. Finally, the Index key designates the order in which Access displays the wizard in the list in the New Object dialog box. The MyCustomForm function, located in the basWizards module, simply calls the frmGetFormInfo form, initiating the wizard process:
Function MyCustomForm(strRecordSource As String) As Variant DoCmd.OpenForm FormName:="frmGetFormInfo", WindowMode:=acDialog End Function