CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Primary Key

  1. #1
    Join Date
    Jul 1999
    Posts
    1

    Primary Key

    How can I set up a primary key in ADO connection to create table ? Thanks



  2. #2
    Join Date
    Jul 1999
    Posts
    104

    Re: Primary Key

    here you have an example of how to make a new table, it also includes a primary key


    Sub NewTable()
    Dim dbs as Database
    Dim tdf as TableDef, fld1 as Field, fld2 as Field
    Dim idx as Index, fldIndex as Field

    ' Return reference to current database.
    set dbs = CurrentDb
    ' Create new table with two fields.
    set tdf = dbs.CreateTableDef("Contacts")
    set fld1 = tdf.CreateField("ContactID", dbLong)
    fld1.Attributes = fld1.Attributes + dbAutoIncrField
    set fld2 = tdf.CreateField("ContactName", dbText, 50)
    ' Append fields.

    tdf.Fields.Append fld1
    tdf.Fields.Append fld2
    ' Create primary key index.
    set idx = tdf.CreateIndex("PrimaryKey")
    set fldIndex = idx.CreateField("ContactID", dbLong)
    ' Append index fields.
    idx.Fields.Append fldIndex
    ' set Primary property.
    idx.Primary = true
    ' Append index.
    tdf.Indexes.Append idx
    ' Append TableDef object.
    dbs.TableDefs.Append tdf
    dbs.TableDefs.Refresh
    set dbs = nothing
    End Sub





  3. #3
    Join Date
    May 1999
    Posts
    3,332

    Re: Primary Key


    conn.execute "create table x( col1 varchar(10) primary key)"



    at least this works for SQLServer.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured