Reading and Setting Properties

 < Day Day Up > 

An object's properties describe the current state of the object. Any class can have zero or more properties. When you instantiate an object from the class, each of these properties has a value. You can read the current value of each property from VBA code, or assign new values to properties from VBA code. Here's an example of reading current values of a form object's properties:

 

 Sub ReadProperties()   ' Display some properties of an object   Dim frm As New Form_Projects   frm.Visible = True   Debug.Print frm.Caption   Debug.Print frm.RecordSelectors End Sub 

When you run this procedure from the Immediate window, you get output that looks this:

 

 Projects True 

To get the value of a property from an object, you refer to it by using the object name and the property name, with a dot in between:

 

 object.property 

You can assign this value to a variable, or (as in the example you just saw) print it, or use it as an argument to a procedure; in fact, you can use a property value anywhere that you can use a simple variable of the same type. In this case, the caption of the form is "Projects" and the form's record selectors (the vertical bar at the left side of the form) are turned on.

To change the value of a property, you assign a value to the property using very similar syntax:

 

 Sub WriteProperties()   ' Change some properties of an object   Dim frm As New Form_Projects   frm.Visible = True   frm.Caption = "Project details"   frm.RecordSelectors = False   MsgBox "Click OK to continue" End Sub 

Running this procedure results in a display like that shown in Figure 8.2. Note that the form's caption is set to the value specified in the code, and the record selectors have been hidden by the VBA code. (Compare Figures 8.1 and 8.2 and you'll see that the small black triangle at the upper-left portion of the form is missing in Figure 8.2 that's the record selector.)

Figure 8.2. Changing property values with VBA.

graphics/08fig02.jpg


NOTE

If you run the WriteProperties procedure and then re-open the form from the regular Access user interface, you'll discover that the property changes made through VBA don't persist. By default, any changes you make in VBA are thrown away by Access when you're done with the object. You can override this default by calling the form's Save method; you learn about methods in the next section of this chapter.


Any object can have three types of properties:

  • Read/write properties, which you can both read and change in code

  • Read-only properties, which you can read, but not change, in code

  • Write-only properties, which you can change, but not read, in code

Most properties are read/write. Read-only properties are often used for properties that can't change after you create an object; for example, a Form object has a Name property that returns the name of the class. Write-only properties are rare. You might run across a class with a password property that is write-only, enabling you to set a new password but not to read an existing password.

     < Day Day Up > 


    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186

    Similar book on Amazon

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