Ask Experts Questions for FREE Help !
Ask
    cuuplate's Avatar
    cuuplate Posts: 4, Reputation: 1
    New Member
     
    #1

    Jun 15, 2009, 09:07 PM
    Error in making primary key through VBA
    I am trying to use VBA code to create a primary key index in an existing database. I am trying this code, but when it gets to the line tbl.Indexes.Append idx I always get the error: Invalid Field Definition idx1 in definition of index or relationship. What am I doing wrong?

    Private Sub Addkey_Click()
    On Error Go to ErrHandler

    Dim db As Database
    Dim tbl As TableDef
    Dim fld As DAO.Field
    Dim idx As DAO.Index

    Tablen = Me.tablenm

    Set db = CurrentDb()
    Set tbl = db.TableDefs(Tablen)
    Set idx = tbl.CreateIndex("Idx1")

    Set fld = idx.CreateField("idx1")
    idx.Fields.Append fld
    idx.Primary = True
    tbl.Indexes.Append idx

    CleanUp:

    Set fld = Nothing
    Set idx = Nothing
    Set tbl = Nothing
    Set db = Nothing

    Exit Sub

    ErrHandler:

    MsgBox "Error in Addkey( )." & vbCrLf & vbCrLf & _
    "Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
    Err.Clear
    Go to CleanUp
    End Sub
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

    Jun 16, 2009, 06:13 AM

    A Primary key is automatically indexed. You don't need to add an index if you set the field as a PK. Try commenting out the offending line and then check the table to see if the field has been designated as a PK.
    cuuplate's Avatar
    cuuplate Posts: 4, Reputation: 1
    New Member
     
    #3

    Jun 16, 2009, 02:32 PM
    Quote Originally Posted by ScottGem View Post
    A Primary key is automatically indexed. You don't need to add an index if you set the field as a PK. Try commenting out the offending line and then check the table to see if the field has been designated as a PK.
    Unfortunately, when I comment out tbl.Indexes.Append idx nothing happens to the database.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Who is Primary [ 1 Answers ]

Is a group insurance policy primary over an individual insurance plan?

Primary tradelines [ 2 Answers ]

How the company set up primary credit tradelines, is aged(seasoned) primary tradelines Can report to credit bureau under my credit? Where can I obtain the information or resources about primary credit tradelines? (any books or web sites) Is there any companies provide tradelines?

98 accord making clunking noise when making hard left turn [ 3 Answers ]

My 98 accord ia making a clunk, clunk, clunk sound when making hard left turns. It only does this with left turns a slow speed like making a u-turn. It actually seems to make the car bounce a little with each clunk. It is definitely not a clicking noise. I have just put a prelude h22 engine with...

Burning error when making dvd's with nero rom [ 17 Answers ]

Hey , I'm not sure if I'm posting this in the right place but I'm not 100% sure where the problem is :( but here goes. Hey guys , I have a problem when I try to burn a dvd. Let me explain what the process I been told to do when burning a dvd. I currently have nero 6.6 I open Nero startsmart...


View more questions Search