Results 1 to 8 of 8

Thread: Set Primary Field (RESOLVED)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    30

    Talking Set Primary Field (RESOLVED)

    Hi Everyone,

    I have an existing Access databse and I would like to Set an existing field as Primary Key field in Table 1 through VB. I am using VB5/DAO.

    Can anyone please Assist..

    All your help will be greatly appreciated.

    Cheers

    Last edited by mohd333; Apr 4th, 2003 at 07:38 AM.

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Take a look at the later posts here:
    http://www.vbforums.com/showthread.p...ight=dao+index

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    30

    How to do

    Thanks PilgrimPete,

    Sorry for being a bit dumb, but I have had a look at the link, and could not realy make any headways...In my case the Table already exists and all I wanto to do is Specify one existing field as as a Primary key (index no duplicates). Could you plz help, do you any simplified sample code...PLZ.plz

    Thankx

  4. #4
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Try this:
    VB Code:
    1. Sub CreateTable()
    2.     Dim tdf As TableDef
    3.     Dim fld As Field
    4.     Dim idx As Index
    5.    
    6.     'grab the tabledef
    7.     Set tdf = CurrentDb.TableDefs("Table1")
    8.        
    9.     'create an index...
    10.     Set idx = New Index
    11.     With idx
    12.         .Name = "idx_PK"
    13.         .Fields.Append .CreateField("existingField")
    14.         .Primary = True
    15.     End With
    16.     '...and add it to the tabledef
    17.     tdf.Indexes.Append idx
    18.    
    19. End Sub

    You'll probably need to tweak it a bit, I haven't tested it...

  5. #5
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    This is clearer (and tested):
    VB Code:
    1. Sub CreatePK()
    2.     Dim db As DAO.Database
    3.     Dim tdf As DAO.TableDef
    4.     Dim fld As DAO.Field
    5.     Dim idx As DAO.Index
    6.    
    7.     'grab the database
    8.     Set db = CurrentDb
    9.    
    10.     'grab the tabledef
    11.     Set tdf = db.TableDefs("Table1")
    12.      
    13.     'create an index...
    14.     Set idx = New Index
    15.     With idx
    16.         .Name = "idx_PK"
    17.         Set fld = .CreateField("PKField")   'this creates a reference to the field
    18.         .Fields.Append fld                  'this adds the field to the index - do this as many times as you need
    19.         .Primary = True                     'this sets the index to be a primary key
    20.     End With
    21.     '...and add it to the tabledef
    22.     tdf.Indexes.Append idx
    23.    
    24. End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    30

    Thumbs up THANKS

    Thanks PilgrimPete,

    I must say Thankyou once more...
    You have helped me on several occaisons, for which I am grateful..

    Whats the best way to call this sub? say if you had this behind a command button.

  7. #7
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    No probs

    What do you mean by the best way to call it?

    Is this the answer, or have I missed the point completely?
    VB Code:
    1. Private sub Button1_Click()
    2.     CreatePK   'create our primary key
    3. End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    30

    Cheers

    That works just fine ...

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