Create a primary key : Primary Key « Access « VBA / Excel / Access / Word






Create a primary key

 

Sub ADOCreatePrimaryKey()

    Dim cat As New ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim pk As New ADOX.Key
    
    cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
       "Data Source=C:\mydb.mdb;"
    
    Set tbl = cat.Tables("Newtable")
    pk.Name = "PrimaryKey"
    pk.Type = adKeyPrimary
    pk.Columns.Append "Column1"
    tbl.Keys.Append pk
End Sub

 








Related examples in the same category

1.Creating a Primary Key
2.Adding a Primary Key to a Table with SQL command
3.Creating a Single-Field Primary Key with SQL command