Adding a Primary Key to a Table with SQL command : Primary Key « Access « VBA / Excel / Access / Word






Adding a Primary Key to a Table with SQL command

 
Sub AddPrimaryKey() 
    Dim conn As ADODB.Connection 
    Dim strTable As String 
    Dim strCol As String 

    On Error GoTo ErrorHandler 
    Set conn = CurrentProject.Connection 

    strTable = "myTable" 
    strCol = "Id" 

    conn.Execute "ALTER TABLE " & strTable & " ADD CONSTRAINT pKey PRIMARY KEY(" & strCol & ");" 
ExitHere: 
    conn.Close 
    Set conn = Nothing 
    Exit Sub 
ErrorHandler: 
    Debug.Print Err.Number & ":" & Err.Description 
    Resume ExitHere 
End Sub 

 








Related examples in the same category

1.Creating a Primary Key
2.Creating a Single-Field Primary Key with SQL command
3.Create a primary key