none
Why CREATE UNIQUE INDEX DUMMY_PK ON " & tableName & " (" & strIndex & "); makes the index primary? RRS feed

  • 问题

  • Hi,

    I use ACCESS2003 to create a table link to the SQL Server table, the script is:

    db.Execute "CREATE UNIQUE INDEX DUMMY_PK ON " & tableName & " (" & strIndex & ");"

    The table tableName has a primary key, but I think it is no problem to create a index, right?

    But once I run this script, there is an error: Primary key already existes.

    I delete the primary key in the table, and try again, OK. The link table created, but the strIndex has been created as index and primary key.

    Why? How can I create the index without making the index the primary key?

    The full code :

                   Dim db As DAO.Database
                    Dim tbldef As DAO.TableDef
                    Dim tblidx As DAO.Index
                    Dim tableName As String
                    
                    tableName = "Student"
                    On Error GoTo ErrHand
                    Set db = CurrentDb
                    Set tbldef = Nothing
                    Set tbldef = db.CreateTableDef(tableName)
                    tbldef.Connect = "ODBC;DSN=MY_DSN;UID=;PWD=;DATABASE=MY_DB"

          
                    tbldef.SourceTableName = tableName
                    db.TableDefs.Append tbldef
                    Dim strIndex As String
                    strIndex = "Name"
                    If Nz(strIndex, "") <> "" Then
                        db.Execute "CREATE UNIQUE INDEX DUMMY_PK ON " & tableName & " (" & strIndex & ");"
                    End If
                    Set db = Nothing

    • 已编辑 linjch 2013年9月2日 7:57
    2013年9月2日 7:53