Results 1 to 6 of 6

Thread: [RESOLVED]Create a Primary Key

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Resolved [RESOLVED]Create a Primary Key

    hi to all:

    This code create a ID field:

    Code:
    Set fld = tdf.CreateField("ID, dbLong)
    fld.Attributes = dbAutoIncrField
    tdf.Fields.Append fld
    and i would like create a primary key too in this field...
    Can anyone tell me the way to do this?

    Thanks
    Last edited by sacramento; Dec 12th, 2008 at 02:13 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Create a Primary Key

    Create a new DAO.Index object, set its properties and then append it to the Indexes collection.

    Code:
    Set fld = tdf.CreateField("ID, dbLong)
    fld.Attributes = dbAutoIncrField
    tdf.Fields.Append fld
    
    Dim idx As DAO.Index
    
    Set idx = New DAO.Index
    idx.Name = "PK_Testing"
    idx.Fields.Append fld
    idx.Primary = True
    
    tdf.Indexes.Append idx

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Create a Primary Key

    Hi:

    I had try your suggestion but VB returns this message:

    Run time error3367
    It's impossible add.One object with this name already exists in the collecion

    the error is in the line market in bold:

    Code:
    Set fld = tdf.CreateField("ID, dbLong)
    fld.Attributes = dbAutoIncrField
    tdf.Fields.Append fld
    
    Dim idx As DAO.Index
    
    Set idx = New DAO.Index
    idx.Name = "PK_Testing"
    idx.Fields.Append fld
    idx.Primary = True
    
    tdf.Indexes.Append idx

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Create a Primary Key

    Moved To Database Development

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Create a Primary Key

    Sorry for the delay in answering.

    I assumed you could use the same Field object in both the Fields and Index collections of the TableDef, guess not. This code works for me

    Code:
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Dim idx As DAO.Index
    
    Set db = DBEngine.OpenDatabase("m:\testing\nwind2.mdb")
    
    Set tdf = New DAO.TableDef
    tdf.Name = "TestTable"
    
    Set fld = tdf.CreateField("Id", dbLong)
    fld.Attributes = dbAutoIncrField
    
    Set idx = tdf.CreateIndex("PK_TestTable")
    idx.Fields.Append idx.CreateField("Id", dbLong)
    idx.Primary = True
    
    tdf.Indexes.Append idx
    tdf.Fields.Append fld
    db.TableDefs.Append tdf
    
    db.Close

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: Create a Primary Key

    Hi:

    Sory too the delay of the replay...

    Yes...your code work...

    Thanks for the help

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