Chapter 25. Using Builders, Wizards, and Menu Add-Ins

<feature><title>In This Chapter</title> </feature>

Why This Chapter Is Important

Add-ins are tools that extend the functionality of Access. They enhance the Access environment by making difficult tasks easier, automating repetitive tasks, and adding enhanced functionality. You can design add-ins for yourself or for others in your organization to use. You even might want to distribute add-ins as part of your application so that your users can build their own database objects. If you are really ambitious, you might decide to build an add-in for sale in the Access third-party market.

Microsoft Access supports three types of add-ins: builders, wizards, and menu add-ins. Each has its own advantages and uses. When you begin the process of designing an add-in, you must decide whether it will be a builder, wizard, or menu add-in. This decision affects how you design the add-in as well as how you install it. This chapter defines and shows you how to design and install each type of add-in.

Using Builders

A builder is an add-in that helps users construct an expression or another data element. Builders most often are used to help users fill in a property of a database object. Builders generally consist of a single dialog box that appears after the user clicks the ellipsis to the right of the property on the Property sheet. An example of a builder is the Expression Builder that appears when users are setting the control source of a text box on a form. Access supports three types of builders:

  • Property builders

  • Control builders

  • Expression builders

Looking at Design Guidelines

When designing your own builder, the design should be consistent with that of the builders included in Access. You therefore must learn about the standards for an Access builder. To design builders that are consistent with the built-in builders, keep a few guidelines in mind:

  1. Set the AutoCenter property of the Builder form to Yes.

  2. Remove record selectors and navigation buttons.

  3. Remove scrollbars.

  4. Be consistent about the placement of objects on the form. Place the OK and Cancel buttons in the same place in each builder you create, for example.

  5. Design the forms as dialog boxes.

Creating a Builder

Now that you are familiar with some general design guidelines for builders, you are ready to design your first builder. What a builder does is completely up to your imagination. For illustration, this section begins with a simple builder that prompts users to select the special effect for a text box. Three overall steps are required to create the builder:

  1. Write a builder function.

  2. Design a builder form.

  3. Register the builder.

The following sections go over each of these steps in detail.

Writing a Builder Function

Writing a Builder Function

The builder function is the function Access calls each time the builder is launched. The function launches the builder form and then returns a value to the appropriate property. Listing 25.1 is an example of a builder function. It is located in CHAP25LIB.MDA in the basBuilders module on the accompanying CD-ROM.

Example 25.1. Creating a Builder Function

Function SpecialEffect(strObject As String, _
            strControl As String, _
            strCurrentValue As String)

   On Error GoTo SpecialEffect_Err

   'Open the special effect form, passing it the special
   'effect currently selected
   DoCmd.OpenForm FormName:="frmSpecialEffect", _
                  WindowMode:=acDialog, _
                  OpenArgs:=strCurrentValue

   'If the user selects a special effect and clicks OK, the
   'form remains open but hidden. Return a value based on
   'which special effect the user selected
   If SysCmd(acSysCmdGetObjectState, acForm, _
            "frmSpecialEffect") = acObjStateOpen Then
      Select Case Forms!frmSpecialEffect.optSpecialEffect.Value
         Case 1
            SpecialEffect = "Flat"
         Case 2
            SpecialEffect = "Raised"
         Case 3
            SpecialEffect = "Sunken"
         Case 4
            SpecialEffect = "Etched"
         Case 5
            SpecialEffect = "Shadowed"
         Case 6
            SpecialEffect = "Chiseled"
      End Select

      'Close the form when done
      DoCmd.Close acForm, "frmSpecialEffect"
   Else

      'If the user clicks cancel, return the original value
      'for the special effect
      SpecialEffect = strCurrentValue
   End If

SpecialEffect_Exit:
   Exit Function

SpecialEffect_Err:
   MsgBox "Error # " & Err.Number & ": " & Err.Description
   Resume SpecialEffect_Exit
End Function

A builder function must receive three preset arguments and must return the value that will become the value for the property being set. The three preset arguments follow:

  • strObject—The name of the table, query, form, report, or module on which the builder is operating

  • strControl—The name of the control to which the property applies

  • strCurrentValue—The current property value

Although the names of the arguments are arbitrary, their data types, positions, and content cannot be changed. Access automatically fills in the values for the three arguments.

The SpecialEffect function opens the form called frmSpecialEffect in Dialog mode, passing it the current value of the property as the OpenArgs value. Figure 25.1 shows the frmSpecialEffect form.

The Special Effect builder form.

Figure 25.1. The Special Effect builder form.

The following code is located in the Click event of the cmdOkay command button on the form:

Private Sub cmdOK_Click()
   Me.Visible = False
End Sub

Notice that the code sets the Visible property of the form to False. The code placed behind the cmdCancel command button looks like this:

Private Sub cmdCancel_Click()
   DoCmd.Close
End Sub

This code closes the frmSpecialEffect form.

After the user clicks OK or Cancel, the code within the SpecialEffect function continues to execute. The function uses the SysCmd function to determine whether the frmSpecialEffect form is loaded. You also can use the user-defined IsLoaded function to accomplish this task. If the frmSpecialEffect form still is loaded, the user must have selected a special effect and clicked OK. Because the form still is open, the function can determine which option button the user selected.

The Case statement within the SpecialEffect function evaluates the value of the optSpecialEffect option group found on the frmSpecialEffect form. It sets the return value for the function equal to the appropriate string, depending on the option button that the user of the builder selects. If the user selects the second option button (with a value of 2), for example, the SpecialEffect function returns the string "Raised". After the option button value is evaluated and the return value is set, the frmSpecialEffect form no longer is needed, so it is closed.

If the user chooses Cancel from the frmSpecialEffect form, the SysCmd function returns False, and the return value of the SpecialEffect function is set equal to strCurrentValue, the original property value. The property value therefore is not changed.

Designing a Builder Form

Although you have seen the code behind the Click event of the OK and Cancel buttons on the frmSpecialEffect form, you have not learned about the design of the form or the idea behind this builder. Ordinarily, when the Special Effect property is set from the Property window, no wizard exists to assist with the process. Although the process of setting the Special Effect property is quite simple, the main problem is that it is difficult to remember exactly what each special effect looks like. The custom special effect builder is designed with this potential problem in mind. It enables users of the builder to see what each special effect looks like before deciding which effect to select.

The properties of the form are quite simple. The Modal property of the form is set to Yes. The record selectors, navigation buttons, and scrollbars are removed. The AutoCenter property of the form is set to True. Six text boxes are included on the form. The special effect of each text box is set to a different style. An option group is included on the form. This group has a different value, depending on which option button is selected. The Default property of the OK command button is set to Yes, making the OK button the default choice. The Cancel property of the Cancel command button is set to Yes, ensuring that if the user presses Esc, the code behind the Cancel button executes. The code behind the Click events of the OK and Cancel buttons were shown in the preceding section. Listing 25.2 shows one more piece of code that enhances this builder.

Example 25.2. Enhancing the Builder

Private Sub Form_Load()
   'Set the Value of the Option Group
   'To the Current Value of the Property
   Select Case Me.OpenArgs
         Case "Flat"
            Me.optSpecialEffect.Value = 1
         Case "Raised"
            Me.optSpecialEffect.Value = 2
         Case "Sunken"
            Me.optSpecialEffect.Value = 3
         Case "Etched"
            Me.optSpecialEffect.Value = 4
         Case "Shadowed"
            Me.optSpecialEffect.Value = 5
         Case "Chiseled"
            Me.optSpecialEffect.Value = 6
End Select
End Sub

This subroutine is placed in the Load event of the builder form. It sets the value of the option group to the current value of the property (passed in as an OpenArg).

Although the frmSpecialEffect form is not particularly exciting, it illustrates quite well that you can design a form of any level of complexity to facilitate the process of setting a property value. So far, though, you have not provided an entry point to the builder. If you select the Special Effect property, no ellipsis appears. You do not yet have access to the builder.

Registering a Builder

Before you can use a builder, you must register it in one of two ways:

  • Manually add the required entries to the Windows registry.

  • Set up the library database so that the Add-in Manager can create the Windows registry entries for you.

Manually Adding Entries to the Windows Registry

Adding the required entries to the Windows registry involves four steps:

  1. If no registry key exists for the property for which you are designing a builder, add the property as a subkey under Property Wizards.

  2. Add an additional subkey for the builder.

  3. Add four predefined registry values for the key.

  4. Set the proper data value for each value name.

The four value names that must be created for the subkey are Can Edit, Description, Function, and Library. Table 25.1 describes these value names for the registry subkey.

Table 25.1. Values for the Registry Subkey

Value Name

Value Type

Purpose

Can Edit

DWORD

Allows the builder to operate on and modify an existing value

Description

String

Specifies a subkey description that appears in the dialog box, which is invoked automatically if more than one builder exists for a property

Function

String

Name of the builder function

Library

String

Name of the library containing the builder function

Now that you have an overview of the steps involved in the process, you are ready to walk through the steps in detail. The following steps set up the builder called SpecialEffect, which is contained in the library database CHAP25LIB.MDA in the folder c:My Libraries:

  1. To invoke the Registry Editor, choose Start|Run from the task bar. Type regedit and click OK. This invokes the Registry Editor.

  2. Locate the HKEY_LOCAL_MACHINESOFTWAREMicrosoftOffice10.0AccessWizardsProperty Wizards key, as shown in Figure 25.2.

    The Property Wizards Registry key.

    Figure 25.2. The Property Wizards Registry key.

  3. Determine whether a subkey exists with the name of the property for which you are creating a builder (in this case, SpecialEffect). If so, skip to step 6.

  4. Choose Edit|New|Key.

  5. Type the property name as the name for the new key (in this case, SpecialEffect).

  6. With the new key selected, choose Edit|New|Key again.

  7. Type a descriptive name for your builder (in this case, SpecialEffectBuilder).

  8. Choose Edit|New|DWORD Value.

  9. Type Can Edit as the value name.

  10. Choose Edit|New|String Value.

  11. Type Description as the value name.

  12. Choose Edit|New|String Value.

  13. Type Function as the value name.

  14. Choose Edit|New|String Value.

  15. Type Library as the value name.

  16. Double-click the Can Edit value name. The Edit DWORD Value dialog box appears, as shown in Figure 25.3.

    The Edit DWORD Value dialog box.

    Figure 25.3. The Edit DWORD Value dialog box.

  17. Enter 1 for the Value data and click OK.

  18. Double-click the Description value name. The Edit String dialog box appears, as shown in Figure 25.4.

    The Edit String dialog box.

    Figure 25.4. The Edit String dialog box.

  19. Enter the description you want the user of the builder to see if more than one builder is assigned to the property (in this case, Special Effect Builder). Click OK.

  20. Double-click the Function value name. Enter the name of the builder function (in this case, SpecialEffect). Click OK.

  21. Double-click the Library value name. Enter the name and location of the library database (in this case, C:My Librarieschap25lib.mda). You do not have to enter the path if the library is located in the Access folder.

Figure 25.5 shows the completed registry entries. The builder now should be ready to use. To test the builder, you need to exit and relaunch Access. If all the registry entries are created successfully, you can use the builder. To test the builder, open any database (other than the library database), create a new form, and add a text box. Select Special Effect from the Format tab of the Properties window. An ellipsis appears to the right of the Special Effect drop-down arrow, as shown in Figure 25.6. If you click the ellipsis, the builder form appears. Select a special effect and click OK. The special effect you selected now appears in the Special Effect property.

The completed registry entries required to add the builder.

Figure 25.5. The completed registry entries required to add the builder.

Using the custom builder.

Figure 25.6. Using the custom builder.

Note

If you do not exactly follow the format for the value names, the message Invalid add-in entry for 'SpecialEffectBuilder' appears, as shown in Figure 25.7. You must correct the registry entry.

This error message appears if the registry entry is invalid.

Figure 25.7. This error message appears if the registry entry is invalid.

Automating the Creation of Registry Entries

The alternative to editing the Windows registry manually is to set up the library database so that the Add-in Manager can create the registry entries for you. This involves adding a table to the library database. The table must be called USysRegInfo. Follow these steps:

  1. Show the system tables. (Tables that begin with USys or MSys are considered system tables and, by default, are hidden.) With the library database open, choose Tools|Options. From the View tab, click System Objects. Click OK. Figure 25.8 shows the database with Tables selected in the Objects list.

    The Tables tab with system objects visible.

    Figure 25.8. The Tables tab with system objects visible.

  2. Import an existing USysRegInfo tableby right-clicking within the database window and selecting Import. Using the Import dialog box, move to the Program FilesMicrosoft OfficeOffice10 folder and locate the ACWZMAIN.MDE file. This is a library file that ships with Access. Select the ACWZMAIN.MDE file and click Import. The Import Objects dialog box appears, as shown in Figure 25.9.

    Using the Import Objects dialog box to add the USysRegInfo table to your library database.

    Figure 25.9. Using the Import Objects dialog box to add the USysRegInfo table to your library database.

  3. Locate and select the USysRegInfo table and click OK. A copy of the USysRegInfo table is added to your library database.

  4. Double-click to open the USysRegInfo table in the database window.

  5. Delete any existing entries in the table.

  6. Add specific entries to the USysRegInfo table. Figure 25.10 shows these entries, and Table 25.2 explains them. Close the table.

    The completed table with entries for registry.

    Figure 25.10. The completed table with entries for registry.

    Table 25.2. The Structure of the USysRegInfo Table

    Field Name

    Description

    SubKey

    Name of the subkey value in the registry where the value you are adding is located

    Type

    Type of subkey value you are creating (String, Binary, or DWORD)

    ValName

    Value name for the entry

    Value

    Value associated with the value name

  7. Open the database that references the add-in.

  8. Choose Tools|Add-ins|Add-in Manager. The Add-in Manager dialog box appears, as shown in Figure 25.11.

    The Add-in Manager dialog box.

    Figure 25.11. The Add-in Manager dialog box.

  9. Click the Add New button to launch the Open dialog box. Here, you can browse for your add-in or select it from within the default folder.

  10. Locate the add-in that you want to add and click Open. The add-in you select is added to the Add-in Manager dialog box and is selected for you.

  11. Click Close. You now are ready to use the add-in.

Using Wizards

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 2002 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

Looking at Design Guidelines

Wizard design guidelines are almost identical to builder design guidelines. The main difference is that wizards generally present the user with multiple modal dialog boxes, whereas a builder generally consists of a single modal dialog box. All the data requirements for the wizard must be met before the user can close the last dialog box.

Creating a Wizard

Creating a Wizard

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.

Step 1 of the custom Form Wizard.

Figure 25.12. Step 1 of the custom Form Wizard.

Step 2 of the custom Form Wizard.

Figure 25.13. Step 2 of the custom Form Wizard.

Each page of the wizard contains code to ensure that it operates successfully. The form is called 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. No other actions are taken because the process is being canceled. 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. The details of the CreateCustomForm function are discussed later in this section. If the function returns True, the wizard form is closed, and a message is displayed indicating that the process was successful. Otherwise, a message is displayed indicating that the form was not created successfully, 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.

Example 25.3. The CreateCustomForm Function Builds the Form

Function CreateCustomForm() As Boolean

   On Error GoTo CreateCustomForm_Err

   Dim frmNew As Form
   Dim ctlNew As Control

   'Create a New Form and Set Several of Its Properties
   Set frmNew = CreateForm()
   frmNew.Caption = Forms!frmGetFormInfo.txtFormCaption
   frmNew.RecordSelectors = False
   frmNew.NavigationButtons = False
   frmNew.AutoCenter = True

   'Create a Label Control on the New Form
   'Set Several of Its Properties
   Set ctlNew = CreateControl(frmNew.Name, acLabel)
   ctlNew.Caption = Forms!frmGetFormInfo.txtLabelCaption
   ctlNew.Width = 3000
   ctlNew.Height = 1000
   ctlNew.Top = 1000
   ctlNew.Left = 1000

   'Evaluate to See if the User Requested an OK Command Button
   'If They Did, Add the Command Button and Set Its Properties
   'Add Click Event Code for the Command Button
   If Forms!frmGetButtons.chkOK.Value = –1 Then
      Set ctlNew = CreateControl(frmNew.Name, acCommandButton)
      ctlNew.Caption = "OK"
      ctlNew.Width = 1000
      ctlNew.Height = 500
      ctlNew.Top = 1000
      ctlNew.Left = 5000
      ctlNew.Name = "cmdOK"
      ctlNew.Properties("OnClick") = "[Event Procedure]"
      frmNew.Module.InsertText "Sub cmdOK_Click()" & vbCrLf & _
         vbTab & "DoCmd.Close acForm, """ & _
         Forms!frmGetFormInfo.txtFormName & _
         """" & vbCrLf & "End Sub"
   End If

   'Evaluate to See if the User Requested a Cancel Command Button
   'If They Did, Add the Command Button and Set Its Properties
   'Add Click Event Code for the Command Button
   If Forms!frmGetButtons.chkCancel.Value = –1 Then
      Set ctlNew = CreateControl(frmNew.Name, acCommandButton)
      ctlNew.Caption = "Cancel"
      ctlNew.Width = 1000
      ctlNew.Height = 500
      ctlNew.Top = 2000
      ctlNew.Left = 5000
      ctlNew.Name = "cmdCancel"
      ctlNew.Properties("OnClick") = "[Event Procedure]"
      frmNew.Module.InsertText "Sub cmdCancel_Click()" & vbCrLf & _
         vbTab & "MsgBox(""You Canceled!!"")" & vbCrLf & "End Sub"
   End If

   'If the User Entered a Form Name, Save the Form
   If Not IsNull(Forms!frmGetFormInfo.txtFormName) Then
      DoCmd.Save , Forms!frmGetFormInfo.txtFormName
   End If

   'Return True If No Errors
   CreateCustomForm = True
   Exit Function

CreateCustomForm_Err:
   MsgBox "Error # " & Err.Number & ": " & Err.Description
   CreateCustomForm = False
   Exit Function
End Function

The code begins by creating both form and control object variables. The form object variable is set to the return value from the CreateForm function. The CreateForm function creates a new form object. Several properties of the new form object are set: Caption, RecordSelectors, NavigationButtons, and AutoCenter. Next, the function uses the CreateControl function to create a new label. A reference to the new label is called ctlNew. The Caption, Width, Height, Top, and Left properties of the new label are set. If the user indicated that he or she wanted an OK button, a new command button is created. The Caption, Width, Height, Top, Left, Name, and Properties properties are all set. The InsertText method is used to insert code for the Click event of the command button. If the user requested a Cancel button, the same properties are set. Finally, if the user indicated a name for the new form, the Save method is used to save the new form object.

Note

Several functions exist to create and delete forms, reports, form controls, and report controls. You can use ADO code to create, modify, and delete tables and queries. Using the functions and ADO code, you can manipulate database objects any way you want. ADO is covered in Chapter 14, “What Are ActiveX Data Objects and Data Access Objects, and Why Are They Important?”

Getting the Wizard Ready to Go

Like a builder, a wizard needs to be added to the Windows registry before it can be used. 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.

Registry entries for the custom Form Wizard.

Figure 25.14. Registry entries 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 the wizard is displayed 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

Using Menu Add-Ins

A menu add-in is a general-purpose tool that enables you to perform a task that generally affects multiple objects or Access itself. The Database Splitter and Database Documenter are examples of menu add-ins. You access menu add-ins through the Add-ins submenu of the Tools menu.

Looking at Design Guidelines

Menu add-ins are available to the user whenever the Tools menu is available. Menu add-ins are not context sensitive like wizards and builders. Therefore, they should in no way rely on what the user is doing at a particular moment.

Creating a Menu Add-In

Creating a menu add-in is just like creating a wizard. The difference is in how you install the add-in. The menu add-in must be registered under HKEY_LOCAL_MACHINESOFTWARE MicrosoftOffice10.0AccessMenu Add-Ins. You can accomplish the registration process by modifying the registry directly or by using the USysRegInfo table. Figure 25.15 shows the registry with the correct entries to run the Form Wizard, created earlier in this chapter, as an add-in. Figure 25.16 shows how you can automate the registration process by using the USysRegInfo table. Three entries are included in the USysRegInfo table. All three entries designate the proper place in the registry tree to add the new key. The first entry contains the subkey and a type of zero. The second entry contains the value name Expression and the name of the entry point function as the value. Notice that the expression name is preceded by an equal sign (=) and is followed by parentheses. The quotation marks within the parentheses are required because this particular entry-point function requires an argument. The third and final entry contains the value name Library and the name of the library as the value. This is all you need to do to turn a wizard into a menu add-in.

Registry entries for the menu add-in.

Figure 25.15. Registry entries for the menu add-in.

The USysRegInfo entries for the menu add-in.

Figure 25.16. The USysRegInfo entries for the menu add-in.

Summary

By creating builders, wizards, and add-ins, you can enhance the development environment for yourself and your users. You even can add wizards so that your users can build their own queries, forms, or reports on-the-fly without a full copy of Access. Your wizard simply needs to prompt the user for the appropriate information and then build the objects to his specifications. What you can do with wizards, builders, and add-ins is limited only by your imagination.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247