Creating a Single-Field Primary Key with SQL command : Primary Key « Access « VBA / Excel / Access / Word






Creating a Single-Field Primary Key with SQL command

 
Sub SingleField_PKey()
    Dim conn As ADODB.Connection
    Dim strTable As String

    On Error GoTo ErrorHandler
    Set conn = CurrentProject.Connection
    strTable = "myTable"

    conn.Execute "CREATE TABLE " & strTable _
        & "(SId INTEGER, " _
        & "SName CHAR (30), " _
        & "CONSTRAINT idxPrimary PRIMARY KEY " _
        & "(SId));"
    Application.RefreshDatabaseWindow
ExitHere:
        conn.Close
        Set conn = Nothing
        Exit Sub
ErrorHandler:
        MsgBox Err.Number & ":" & Err.Description
        Resume ExitHere
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.Create a primary key