Create Custom Methods for Existing Objects


In addition to custom procedures, custom methods can also be created for existing objects by using public sub procedures or functions.

Using Public Sub Procedures and Functions

Custom methods can be created just as you created methods for your own objects, only you add a public sub procedure or function to the existing object. Now add a new method for the frmCarDetails form to see how this works.

Try It Out-Adding New Method for the frmCarDetails Form

image from book
  1. From the Visual Basic Editor, select the frmCarDetails class module. Add the following method to the class:

      Public Sub TestMethod() MsgBox "This is a test method added to an existing object" End Sub 

  2. From the frmCarDetails class module, add a new procedure called DemonstrateCustomMethods and type the me keyword followed by a period, as shown in Figure 4-16.

    image from book
    Figure 4-16

How It Works

First, you added the new method to the existing frmCarDetails form. Then, when you referred to frmCarDetails in code using the me keyword, you saw that the custom method you just added was in the drop-down list along with the custom property you just created and the original properties and methods. Just like properties, methods can be added to nearly any existing object.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net