Primary 屬性

適用於:Access 2013 | Access 2016

使用 Primary 屬性來指定資料表的主鍵欄位。 主鍵欄位會保存可唯一識別資料表中每筆記錄的資料。

設定

Primary 屬性使用下列設定值。

設定 Visual Basic 描述
True 選取的索引是主索引鍵。
False 選取的索引不是主索引鍵。

設定 Primary 屬性的方法有三種:

  • 在 [設計檢視] 資料表中,依您想要的主鍵順序選取欄位,然後按一下工具列上的 [ 主鍵 ]。

  • 在 [索引] 視窗中,選取或輸入 [索 引名稱 ] 資料行中的索引名稱,然後在 [索引屬性] 區段中將 [ 主要 ] 屬性設定為 [是]。

  • 在 Visual Basic 中,若要存取索引的 Primary 屬性,請使用 DAO PrimaryKey 屬性。

註解

Microsoft Access 會自動在資料表的索引鍵欄位中建立索引,並將之用來搜尋記錄及建立資料表間的連接。 必須在每一個主索引鍵欄位中輸入主索引鍵索引,且不允許重複。 多欄位之主索引鍵的欄位順序,會決定資料表的預設排序順序。

If there is no primary key when you save a table's design, Microsoft Access will display a dialog box asking whether you want a primary key to be created. If you click Yes, an AutoNumber data type field will be added to the table (with its NewValues property set to Increment) and set as the primary key. If you click No, no primary key will be created.

沒有主索引鍵的資料表不可用於建立關聯,且從中進行排序和搜尋的速度也會較慢。

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應