Database.CreateProperty 方法 (DAO)
适用于:Access 2013、Office 2013
创建一个新的用户定义的 Property 对象(仅适用于 Microsoft Access 工作区)。 .
语法
表达式 。CreateProperty (名称、 类型、 值、 DDL)
表达式 一个表示 Database 对象的变量。
参数
名称 |
必需/可选 |
数据类型 |
说明 |
---|---|---|---|
Name |
可选 |
Variant |
一个对新的 Property 对象进行唯一命名的 String。 有关有效 Property 名称的详细信息,请参阅 Name 属性。 |
Type |
可选 |
Variant |
一个定义新的 Property 对象的数据类型的常量。 有关有效数据类型的信息,请参阅 Type 属性。 |
值 |
可选 |
Variant |
一个包含初始属性值的 Variant。 有关详细信息,请参阅 Value 属性。 |
Ddl |
可选 |
Variant |
一个 Variant(Boolean 子类型),用于指示 Property 是否为 DDL 对象。 默认值为 False。 如果 DDL 为 True,则用户无法更改 或删除此属性对象 ,除非他们具有 dbSecWriteDef 权限。 |
返回值
属性
备注
只能在某个对象的永久 Properties 集合中创建用户定义的 Property 对象。
如果使用 CreateProperty 时省略了一个或多个可选部分,则可以在将新对象追加到集合之前,使用适当的赋值语句设置或重置相应的属性。 追加对象后,可以改动此对象的某些(但不是所有)属性设置。 有关详细信息,请参阅 Name、 Type 和 Value 属性主题。
如果 name 引用已是集合成员的对象,则使用 Append 方法时将发生运行时错误。
若要从集合中删除用户定义的 Property 对象,请对 Properties 集合使用 Delete 方法。 不能删除内置属性。
注意
如果省略 DDL 参数,则默认为 False (非 DDL) 。 由于没有公开相应的 DDL 属性,必须删除要从 DDL 更改为非 DDL 的 Property 对象,然后重新创建该对象。
示例
以下示例尝试设置用户定义的属性的值。 如果该属性不存在,则使用 CreateProperty 方法创建一个新属性并设置其值。 若要使该过程运行,需要使用 SetProperty 过程。
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