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

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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



Creating Custom Properties and Methods


Forms and reports are

Class modules , which means they act as templates for objects you create instances of at runtime. Public procedures of a form and report become Custom properties and methods of the form object at runtime. Using VBA code, you can set the values of a form's Custom properties and execute its methods.

Creating Custom Properties


You can create Custom properties of a form or report in one of two ways:

  • Create Public variables in the form or report.

  • Create PropertyLet and PropertyGet routines.


Creating and Using a Public Variable as a Form Property

The following steps are used to create and access a Custom form or report property based on a Public variable. The example is included in CHAP9EX.MDB in the forms frmPublicProperties and frmChangePublicProperty.


  • Begin by creating the form that will contain the Custom property (Public variable).

  • Place a Public variable in the General Declarations section of the form or report (see Figure 9.33).

    Figure 9.33. Creating a Public variable in the General Declarations section of a Class module.

  • Place code in the form or report that accesses the Public variable. The code in Figure 9.33 creates a Public variable called CustomCaption. The code behind the Click event of the cmdChangeCaption command button sets the form's (frmPublicProperties) Caption property equal to the value of the Public variable.

  • Create a form, report, or module that modifies the value of the Custom property. Figure 9.34 shows a form called frmChangePublicProperty.

    Figure 9.34. Viewing the frmChangePublicProperty form.

  • Add the code that modifies the value of the Custom property. The code behind the ChangeCaption button, as seen in Figure 9.33, modifies the value of the Custom property called CustomCaption that's found on the frmPublicProperties form.


  • To test the Custom property created in the preceding example, run the frmPublicProperties form, which is in the CHAP9EX.MDB database on your sample code CD-ROM. Click the Change Form Caption command button. Nothing happens, because the value of the Custom property hasn't been set. Open the frmChangePublicProperty form and click the Change Form Property command button. Return to frmPublicProperties and again click the Change Form Caption command button. The form's caption should now change.

    Close the frmPublicProperties form and try clicking the Change Form Property command button. A runtime error occurs indicating that the form you're referring to is not open. You can eliminate the error by placing the following code in the Click event of cmdPublicFormProperty:

    Private Sub cmdPublicFormProperty_Click()
    Form_frmPublicProperties.CustomCaption = _
    "This is a Custom Caption"
    Forms_frmPublicProperties.Visible = True
    End Sub

    This code modifies the value of the Public property by using the syntax Form_FormName.Property. If the form isn't loaded, this syntax loads the form but leaves it hidden. The next command sets the form's Visible property to True.

    Creating and Using Custom Properties with PropertyLet and PropertyGet Routines

    A PropertyLet routine is a special type of subroutine that automatically executes whenever the property's value is changed. A PropertyGet routine is another special subroutine that automatically executes whenever the value of the Custom property is retrieved. Instead of using a Public variable to create a property, you insert two special routines: PropertyLet and PropertyGet. This example is found in CHAP9EX.MDB in the frmPropertyGetLet and frmChangeWithLet forms. To insert the PropertyLet and PropertyGet routines, follow these steps:


  • Choose Insert, Procedure. The dialog box shown in Figure 9.35 appears.

    Figure 9.35. Starting a new procedure with the Add Procedure dialog box.

  • Type the name of the procedure in the Name text box.

  • Select Property from the Type option buttons.

  • Select Public as the Scope so that the property is visible outside the form.

  • Click OK. The PropertyGet and PropertyLet subroutines are inserted in the module (see Figure 9.36).

    Figure 9.36. The PropertyGet and PropertyLet subroutines inserted in the module.


  • Notice that the Click event code for the cmdChangeCaption command button hasn't changed. The PropertyLet routine, which automatically executes whenever the value of the CustomCaption property is changed, takes the uppercase value of what it's being sent and places it in a Private variable called mstrCustomCaption. The PropertyGet routine takes the value of the Private variable and returns it to whoever asked for the value of the property. The following code is placed in the form called frmChangeWithLet:

    Private Sub cmdPublicFormProperty_Click()
    Form_frmPropertyGetLet.CustomCaption = "This is a Custom Caption"
    Forms!frmPropertyGetLet.Visible = True
    End Sub

    This routine tries to set the value of the Custom property called CustomCaption to the value "This is a Custom Caption". Because the property's value is being changed, the PropertyLet routine in frmPropertyGetLet is automatically executed. It looks like this:

    Public Property Let CustomCaption(ByVal CustomCaption As String)
    mstrCustomCaption = UCase$(CustomCaption)
    End Property

    The PropertyLet routine receives the value "This is a Custom Caption" as a parameter. It uses the UCase function to manipulate the value it was passed and convert it to uppercase. It then places the manipulated value into a Private variable called mstrCustomCaption. The PropertyGet routine isn't executed until the user clicks the cmdChangeCaption button in the frmPropertyGetLet form. The Click event of cmdChangeCaption looks like this:

    Private Sub cmdChangeCaption_Click()
    Me.Caption = CustomCaption
    End Sub

    Because this routine needs to retrieve the value of the Custom property CustomCaption, the PropertyGet routine automatically executes:

    Public Property Get CustomCaption() As String
    CustomCaption = mstrCustomCaption
    End Property

    The PropertyGet routine takes the value of the Private variable, set by the PropertyLet routine, and returns it as the value of the property.

    You might wonder why this method is preferable to declaring a Public variable. Using the UCase function within PropertyLet should illustrate why. Whenever you expose a Public variable, you can't do much to validate or manipulate the value you receive. The PropertyLet routine gives you the opportunity to validate and manipulate the value to which the property is being set. By placing the manipulated value in a Private variable and then retrieving the Private variable's value when the property is returned, you gain full control over what happens internally to the property.Chapter 13, "Exploiting the Power of Class Modules."

    Creating Custom Methods


    Custom methods are simply Public functions and subroutines placed in a form module or a report module. As you will see, they can be called by using the Object.Method syntax. Here are the steps involved in creating a Custom method; they are found in CHAP9EX.MDB in the forms frmMethods and frmExecuteMethod:


  • Open the form or report that will contain the Custom method.

  • Create a Public function or subroutine (see Figure 9.37).

    Figure 9.37. Using the custom method ChangeCaption.

  • Open the form module, report module, or code module that executes the Custom method.

  • Use the Object.Method syntax to invoke the Custom method (see Figure 9.38).

    Figure 9.38. The Click event code behind the Execute Method button.


  • Figure 9.37 shows the Custom method ChangeCaption found in the frmMethods form. The method changes the form's caption. Figure 9.38 shows the Click event of cmdExecuteMethod found in the frmExecuteMethod form. It issues the ChangeCaption method of the frmMethods form, and then sets the form's Visible property to True.


    / 544