Property Object, Properties Collection

Property Object, Properties Collection (MDB)

Several types of properties exist in Microsoft Access. Each of these properties can be represented in Visual Basic code by a Property object variable, and each is a member of a Properties collection.

Properties that Apply to Data Access Objects

Microsoft Access–defined properties that apply to DAO objects differ from properties defined by the Jet database engine in several ways.

To refer to a user-defined property or a property defined by Microsoft Access, you must explicitly refer to the Properties collection. The fastest way to refer to a Microsoft Access property is by the following syntax:

object.Properties!propertyname

In the preceding example, object is a DAO object and propertyname is the name of a Microsoft Access property.

You can also use the following, slightly slower syntax:

object.Properties("name")

In contrast, to refer to properties defined by the Jet database engine, you can simply use the object.name syntax.

If you are setting the value of a Microsoft Access-defined property for the first time, you first need to create it by using the CreateProperty method. For example, the Caption property of a Field object is a Microsoft Access–defined property. If you haven't previously set the Caption property in table Design view, but are setting the property for the first time from Visual Basic code, you must first create that property by using the CreateProperty method and append it to the Properties collection before you can set its value.

A Microsoft Access–defined property is automatically appended to the Properties collection the first time it is set in the Microsoft Access window, so if you have already set a property in the user interface, you don't need to create and append the property in code. For example, you can set the Caption property for a table in Datasheet view by clicking Font on the Format menu. This property is then included in the Properties collection of a TableDef object that points to the table.

Until you have set a Microsoft Access–defined property either in table Design view or from Visual Basic code, that property won't appear in the Properties collection. When you set these properties from Visual Basic, you should include error-handling code that checks to see if the property exists in the Properties collection, and creates and appends the property if it doesn't.

Properties that Apply to Microsoft Access Objects

Like DAO objects, every Microsoft Access object contains a Properties collection, which has built-in Property objects. For example, Property objects that apply to a form are members of the Properties collection of the Form object.

You can also create user-defined properties for Microsoft Access objects. For example, you might create a property called TextType that applies to a text box control.

Property objects in the Properties collections of Form, Report, and Control objects differ from DAO Property objects in that they don't have an Inherited property.

You can enumerate the Property objects in the Properties collections of Form, Report, and Control objects. However, the Microsoft Access Application object and the Screen object have Properties collections that can't be enumerated. Additionally, the properties of these objects are read-only.