Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources] - نسخه متنی

Alison Balter

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



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:

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

  • Remove record selectors and navigation buttons.

  • Remove scrollbars.

  • 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.

  • 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:


  • Write a builder function.

  • Design a builder form.

  • Register the builder.


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

    Writing a Builder Function

    The

    builder function is the function Access calls each time you launch the builder. 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.

    Listing 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 operates

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

    • strCurrentValue
      The current property value


    Although the names of the arguments are arbitrary, you cannot change their data types, positions, and content. 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.

    Figure 25.1. Here we see 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 is still 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 code evaluates the option button value and sets the return value, the code no longer needs the frmSpecialEffect form, so the code then closes the form.

    If the user chooses Cancel from the frmSpecialEffect form, the SysCmd function returns False, and the code sets the return value of the SpecialEffect function equal to strCurrentValue, the original property value. In this case, the code does not change the property value.

    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 you set the SpecialEffect property from the Property window, no wizard exists to assist with the process. Although the process of setting the SpecialEffect property is quite simple, the main problem is that it is difficult to remember exactly what each special effect looks like. I designed the custom special effect builder 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. I have set the Modal property of the form to Yes. I removed the record selectors, navigation buttons, and scrollbars. I set the AutoCenter property of the form to True. I included six text boxes on the form. I set the special effect of each text box to a different style. The form includes an option group. This group has a different value, depending on which option button the user selects. The code sets the Default property of the OK command button to Yes, making the OK button the default choice. The code sets the Cancel property of the Cancel command button to Yes, ensuring that if the user presses Esc, the code behind the Cancel button executes. The preceding section showed the code behind the Click events of the OK and Cancel buttons. Listing 25.2 shows one more piece of code that enhances this builder.

    Listing 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

    I added this subroutine to 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 SpecialEffect 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:


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

  • Add an additional subkey for the builder.

  • Add four predefined registry values for the key.

  • Set the proper data value for each value name.


  • You must create four value names for the subkey. They 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:


  • To invoke the Registry Editor, choose Start, Run from the task bar. Type

    regedit and click OK. This invokes the Registry Editor.

  • Locate the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\11.0\Access\Wizards\Property Wizards key, as shown in Figure 25.2.

    Figure 25.2. Use the Registry Editor to invoke the Property Wizards Registry key.

  • 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.

  • Choose Edit, New, Key.

  • Type the property name as the name for the new key (in this case,

    SpecialEffect ).

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

  • Type a descriptive name for your builder (in this case,

    SpecialEffectBuilder ).

  • Choose Edit, New, DWORD Value.

  • Type

    Can Edit as the value name.

  • Choose Edit, New, String Value.

  • Type

    Description as the value name.

  • Choose Edit, New, String Value.

  • Type

    Function as the value name.

  • Choose Edit, New, String Value.

  • Type

    Library as the value name.

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

    Figure 25.3. Double-click the Can Edit value name to bring up the Edit DWORD Value dialog box.

  • Enter

    1 for the Value data and click OK.

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

    Figure 25.4. The Edit String dialog box appears when you double-click the Description value name.

  • Enter the description you want the user of the builder to see if he has more than one builder assigned to the property (in this case,

    Special Effect Builder ). Click OK.

  • Double-click the Function value name. Enter the name of the builder function (in this case,

    SpecialEffect ). Click OK.

  • Double-click the Library value name. Enter the name and location of the library database (in this case,

    C:\Libraries\chap25lib.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 you successfully created all the registry entries, 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 SpecialEffect property.

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


    Figure 25.6. Using the custom builder.


    NOTE

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

    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. You must call the table USysRegInfo. Follow these steps:


  • Show the system tables. (Access considers tables that begin with USys or MSys system tables and, by default, hides them.) 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.

    Figure 25.8. Here is the Tables tab with system objects visible.

  • Import an existing USysRegInfo table by right-clicking within the database window and selecting Import. Using the Import dialog box, move to the \Program Files\Microsoft Office 11\Office11 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.

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

  • Locate and select the USysRegInfo table and click OK. Access adds a copy of the USysRegInfo table to your library database.

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

  • Delete any existing entries in the table.

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

    Figure 25.10. The completed table with entries for registry.

  • Open the database that references the add-in.

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

    Figure 25.11. The Add-in Manager dialog box.

  • 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.

  • Locate the add-in that you want to add and click Open. Access adds the add-in you select to the Add-in Manager dialog box and selects the add-in for you.

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


  • 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


    / 544