共用方式為


DataRecordset.GetPrimaryKey method (Visio)

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

注意事項

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

語法

表情GetPrimaryKey (PrimaryKeySettingsPrimaryKey () )

一個回傳 DataRecordset 物件的表達式。

參數

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

傳回值

註解

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

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

常數 描述
visKeyRow 順序 1 使用資料列順序當做主索引鍵。
visKeySingle(視覺鍵單曲) 2 使用單一資料欄當做主索引鍵資料欄。
visKeyComposite 3 使用多個資料欄當做主索引鍵資料欄。

對於 PrimaryKey () out 參數,傳遞一個無量綱的字串陣列。 若回傳的主鍵設定為 visKeySinglevisKeyComposite,該方法也會回傳 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 支援與意見反應