Freigeben über


Properties Collection (DAO)

A Properties collection contains all the Property objects for a specific instance of an object.

Remarks

Every DAO object except the Connection and Error objects contains a Properties collection, which has certain built-in Property objects. These Property objects (which are often just called properties) uniquely characterize that instance of the object.

In addition to the built-in properties, you can also create and add your own user-defined properties. To add a user-defined property to an existing instance of an object, first define its characteristics with the CreateProperty method, then add it to the collection with the Append method. Referencing a user-defined Property object that has not yet been appended to a Properties collection will cause an error, as will appending a user-defined Property object to a Properties collection containing a Property object of the same name.

You can use the Delete method to remove user-defined properties from the Properties collection, but you can't remove built-in properties.

Note

A user-defined Property object is associated only with the specific instance of an object. The property isn't defined for all instances of objects of the selected type.

To refer to a built-in Property object in a collection by its ordinal number or by its Name property setting, use any of the following syntax forms:

object.Properties(0)

object.Properties("name")

object.Properties![name]

For a built-in property, you can also use this syntax:

object.name

Note

For a user-defined property, you must use the full object.Properties("name") syntax.

With the same syntax forms, you can also refer to the Value property of a Property object. The context of the reference will determine whether you are referring to the Property object itself or the Value property of the Property object.

Example

This example creates a user-defined property for the current database, sets its Type and Value properties, and appends it to the Properties collection of the database. Then the example enumerates all properties in the database.

Sub PropertyX() 
 
 Dim dbsNorthwind As Database 
 Dim prpNew As Property 
 Dim prpLoop As Property 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 
 With dbsNorthwind 
 ' Create and append user-defined property. 
 Set prpNew = .CreateProperty() 
 prpNew.Name = "UserDefined" 
 prpNew.Type = dbText 
 prpNew.Value = "This is a user-defined property." 
 .Properties.Append prpNew 
 
 ' Enumerate all properties of current database. 
 Debug.Print "Properties of " & .Name 
 For Each prpLoop In .Properties 
 With prpLoop 
 Debug.Print " " & .Name 
 Debug.Print " Type: " & .Type 
 Debug.Print " Value: " & .Value 
 Debug.Print " Inherited: " & _ 
 .Inherited 
 End With 
 Next prpLoop 
 
 ' Delete new property because this is a 
 ' demonstration. 
 .Properties.Delete "UserDefined" 
 End With 
 
End Sub 

This example tries to set the value of a user-defined property. If the property doesn't exist, it uses the CreateProperty method to create and set the value of the new property. The SetProperty procedure is required for this procedure to run.

Sub CreatePropertyX() 
 
 Dim dbsNorthwind As Database 
 Dim prpLoop As Property 
 
 Set dbsNorthwind = OpenDatabase("Northwind.mdb") 
 
 ' Set the Archive property to True. 
 SetProperty dbsNorthwind, "Archive", True 
 
 With dbsNorthwind 
 Debug.Print "Properties of " & .Name 
 
 ' Enumerate Properties collection of the Northwind 
 ' database. 
 For Each prpLoop In .Properties 
 If prpLoop <> "" Then Debug.Print " " & _ 
 prpLoop.Name & " = " & prpLoop 
 Next prpLoop 
 
 ' Delete the new property since this is a 
 ' demonstration. 
 .Properties.Delete "Archive" 
 
 .Close 
 End With 
 
End Sub 
 
Sub SetProperty(dbsTemp As Database, strName As String, _ 
 booTemp As Boolean) 
 
 Dim prpNew As Property 
 Dim errLoop As Error 
 
 ' Attempt to set the specified property. 
 On Error GoTo Err_Property 
 dbsTemp.Properties("strName") = booTemp 
 On Error GoTo 0 
 
 Exit Sub 
 
Err_Property: 
 
 ' Error 3270 means that the property was not found. 
 If DBEngine.Errors(0).Number = 3270 Then 
 ' Create property, set its value, and append it to the 
 ' Properties collection. 
 Set prpNew = dbsTemp.CreateProperty(strName, _ 
 dbBoolean, booTemp) 
 dbsTemp.Properties.Append prpNew 
 Resume Next 
 Else 
 ' If different error has occurred, display message. 
 For Each errLoop In DBEngine.Errors 
 MsgBox "Error number: " & errLoop.Number & vbCr & _ 
 errLoop.Description 
 Next errLoop 
 End 
 End If 
 
End Sub