DataRecordset.GetPrimaryKey 方法 (Visio)

會取得資料錄集的主索引鍵設定及主索引鍵資料欄名稱。

注意事項

This Visio object or member is available only to licensed users of Visio Professional 2013.

語法

運算式。GetPrimaryKey (PrimaryKeySettings、 PrimaryKey () )

運算式 傳回 DataRecordset 物件的運算式。

參數

名稱 必要/選用 資料類型 描述
PrimaryKeySettings 必要 VisPrimaryKeySettings 輸出參數。 資料記錄集的主索引鍵設定。 如需可能的值,請參閱<備註>。
PrimaryKey () 必要 字串 輸出參數。 String 變數的陣列。

傳回值

無

註解

使用 GetPrimaryKey 方法來決定資料記錄集的現有主索引鍵設定,以及如果已指定主索引鍵,則決定一欄或多欄的主索引鍵名稱。 此方法會在 PrimaryKeySettings 輸出參數中傳回資料記錄集的主索引鍵設定,做為 VisPrimaryKeySettings 列舉中的值。 預設值 (若未指定主索引鍵) 為 visKeyRowOrder,這表示 Microsoft Visio 會依據資料列順序識別資料記錄集列。

已指定主索引鍵的資料記錄集可以有單一或複合主索引鍵欄。 單一資料欄主索引鍵會根據單一資料欄中的值來識別資料列。 綜合主索引鍵會使用兩個以上的資料欄來唯一識別資料列。 下表顯示 PrimaryKeySettings 的可能值。

常數 值 描述
visKeyRowOrder 1 使用資料列順序當做主索引鍵。
visKeySingle 2 使用單一資料欄當做主索引鍵資料欄。
visKeyComposite 3 使用多個資料欄當做主索引鍵資料欄。

針對 PrimaryKey () out 參數,傳遞字串的無維度陣列。 如果傳回的主索引鍵設定為 visKeySingle 或 visKeyComposite,此方法也會在 PrimaryKey () out 參數中傳回主索引鍵資料行名稱字串的陣列。 若主索引鍵設定為預設的 visKeyRowOrder,則方法會傳回空陣列。

使用 DataRecordset.SetPrimaryKey 方法指定資料記錄集的主索引鍵設定,以及您想要設為主索引鍵的欄或欄名稱。 設定主索引鍵時,請確定您挑選為主索引鍵資料欄的資料欄,在每個資料列上都含有唯一的值 (或值組)。

範例

此Microsoft Visual Basic for Applications (VBA) 巨集 示範如何使用 GetPrimaryKey 方法來決定資料記錄集的主索引鍵設定,以及第一個主索引鍵資料行的名稱。 巨集會尋找最近建立與文件相關聯的資料記錄集,如果已指定主索引鍵,則會在 [立即] 視窗中列印資料記錄集的主索引鍵設定值及第一個主索引鍵欄的名稱。 如果沒有主索引鍵,它會列印主索引鍵設定和陳述式「沒有主索引鍵」。

執行此巨集之前,請在目前文件中至少建立一個資料記錄集,如果需要,請使用 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 支援與意見反應。