DataRecordset.GetPrimaryKey 方法 (Visio)
获取数据记录集的主关键字设置以及主关键字列的名称。
注意
此 Visio 对象或成员仅供 Visio Professional 2013 的授权用户使用。
语法
表达式。GetPrimaryKey (PrimaryKeySettings, PrimaryKey () )
表达 返回 DataRecordset 对象的表达式。
参数
名称 | 必需/可选 | 数据类型 | 说明 |
---|---|---|---|
PrimaryKeySettings | 必需 | VisPrimaryKeySettings | 输出参数。 数据记录集的主关键字设置。 请参阅“注解”来了解可能的值。 |
PrimaryKey () | 必需 | 字符串 | out 参数。 String 类型的变量组成的数组。 |
返回值
Nothing
注解
使用 GetPrimaryKey 方法可以确定数据记录集的现有主键设置以及主键列的名称(如果已指定主键)。 该方法在 PrimaryKeySettings 输出参数中将数据记录集的主关键字设置作为 VisPrimaryKeySettings 枚举中的一个值返回。 默认设置(如果尚未指定主关键字)是 visKeyRowOrder,这意味着 Microsoft Visio 按行顺序标识数据记录集。
已为其指定主键的数据记录集可以包含单列或复合主键列。 单列主关键字根据单个列中的值标识行。 复合主键使用两个或多个列唯一地标识一行。 下表列出了 PrimaryKeySettings 的可能值。
常量 | 值 | Description |
---|---|---|
visKeyRowOrder | 1 | 使用行顺序作为主关键字。 |
visKeySingle | 2 | 使用单个列作为主关键字列。 |
visKeyComposite | 3 | 使用多个列作为主关键字列。 |
对于 PrimaryKey () out 参数,请传递字符串的无维度数组。 如果返回的主键设置是 visKeySingle 或 visKeyComposite,则 该方法还会返回 PrimaryKey () out 参数中的主键列名称字符串数组。 如果主关键字设置是 visKeyRowOrder(即默认值),则该方法返回一个空数组。
使用 DataRecordset.SetPrimaryKey 方法可指定数据记录集的主键设置,以及要设置为主键列或列的列的名称。 在设置主关键字时,请确保要选作主关键字列的列在每一行中都包含唯一值(或值集)。
示例
以下 Microsoft Visual Basic for Applications (VBA) 宏说明如何使用 GetPrimaryKey 方法确定数据记录集的主关键字设置以及第一个主关键字列的名称。 该宏查找与文档关联的最近创建的数据记录集,如果已指定主键,则会在“即时”窗口中打印主键设置的值和数据记录集的第一个主键列的名称。 如果不存在主关键字,则输出主关键字设置和“No primary key”语句。
运行此宏之前,请在当前文档中至少创建一个数据记录集,如果需要,请使用 SetPrimaryKey 方法指定主键。
Public Sub GetPrimaryKey_Example()
Dim vsoDataRecordset As Visio.DataRecordset
Dim intCount As Integer
Dim astrPrimaryKeyColumns() As String
Dim vsoKeySettings As VisPrimaryKeySettings
intCount = ThisDocument.DataRecordsets.Count
Set vsoDataRecordset = ThisDocument.DataRecordsets(intCount)
vsoDataRecordset.GetPrimaryKey vsoKeySettings, astrPrimaryKeyColumns
If vsoKeySettings = visKeyRowOrder Then
Debug.Print vsoKeySettings, "No primary key"
Else
Debug.Print vsoKeySettings, astrPrimaryKeyColumns(0)
End If
End Sub
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。