>
object.Properties!nameYou 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 using the CreateProperty method. For example,
the Caption property of a Field object is a
Microsoft Access-defined property. If you have not 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 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 will not 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 does not.
Function EnumerateProperty () As Integer
Dim wrkDefault As Workspace, dbsExample As Database
Dim prpUserDefined As Property, prpEnum As Property
Dim I As Integer
' Get default workspace and current database.
Set wrkDefault = DBEngine.Workspaces(0)
Set dbsExample = wrkDefault.OpenDatabase("Northwind.mdb")
' Create user-defined property.
Set prpUserDefined = dbsExample.CreateProperty()
' Set properties of new property.
prpUserDefined.Name = "UserDefinedProperty"
prpUserDefined.Type = dbText
prpUserDefined.Value = "This is a user-defined property."
' Append property to current database.
dbsExample.Properties.Append prpUserDefined
' Enumerate all properties of current database.
Debug.Print "Properties of Database "; dbsExample.Name
For I = 0 To dbsExample.Properties.Count - 1
Set prpEnum = dbsExample.Properties(I)
Debug.Print
Debug.Print " Properties("; I; ")"
Debug.Print " Name: "; prpEnum.Name
Debug.Print " Type: "; prpEnum.Type
Debug.Print " Value: "; prpEnum.Value
Debug.Print " Inherited: "; prpEnum.Inherited
Next I
Debug.Print
EnumerateProperty = True
End Function
This example shows how you can set an
application-defined property (or any user-defined property that
may not yet exist) without causing a run-time error. The example
sets an arbitrary property of a Field object. The return
value of the function is True if the value was properly
set. The return value is False if an unexpected error
occurs when the property is set.
Function SetFieldProperty (fldPropVal As Field, strName As String, intType As Integer, varValue As Variant) As Integer
Const ERR_PROPERTY_NONEXISTENT = 3270
Dim prpUserDefined As Property
On Error Resume Next ' Function handles errors.
SetFieldProperty = True
fldPropVal.Properties(strName) = varValue
If Err <> 0 Then ' Error occurred when value was set.
If Err <> ERR_PROPERTY_NONEXISTENT
On Error GoTo 0
SetFieldProperty = False
Else
' Create Property object, setting its Name, Type, and Value
' properties.
On Error Resume Next
Set prpUserDefined = fldPropVal.CreateProperty(strName, _
intType, varValue)
fldPropVal.Properties.Append prpUserDefined
If Err <> 0 Then
SetFieldProperty = False
End If
On Error GoTo 0
End If
End If
End Function
Example (Microsoft Access)
The following example creates a new user-defined
property, sets its initial value, and appends it to the Properties
collection of a TableDef object.
Sub CreateNewProperty()
Dim dbs As Database, tdf As TableDef
Dim prp As Property
' Return Database object pointing to current database.
Set dbs = CurrentDb
Set tdf = dbs.TableDefs!Orders
' Create new property, denote type, and set initial value.
Set prp = tdf.CreateProperty("LastSaved", dbText, "New")
' Append to Properties collection of TableDef object.
tdf.Properties.Append prp
End Sub
The next example creates a property that is defined
by Microsoft Access, but applies to data access objects. Because
the Microsoft Jet database engine cannot recognize properties
defined by Microsoft Access, you must create a new Property
object and append it to the Properties collection if you
are setting the property for the first time.
Note that you must specify the correct constant for
the type argument when you create the property. If you're
not certain which data type you should use, see the topic for the
individual property.
Sub CreateAccessProperty()
Dim dbs As Database, tdf As TableDef
Dim prp As Property
' Return Database object pointing to current database.
Set dbs = CurrentDb
Set tdf = dbs.TableDefs!Orders
' Create property, denote type, and set initial value.
Set prp = tdf.CreateProperty("DatasheetFontItalic", dbBoolean, True)
' Append Property object to Properties collection.
tdf.Properties.Append prp
End Sub