Need some expert eyes on adding primary key to a linked table via VBA module. MS Access at version 97.
When following code executed, recieve error message: Run time error'3367':
Can't Append. An object with the name already exists in the collection.
Option Compare Database
Option Explicit
Dim db As Database
Dim tdf As TableDef
Dim strDSNName As String
Dim strTableName As String
Dim Idx As Index
Dim Fld As Field
'
Sub BuildTableLink()
Set db = CurrentDb
Set strTableName = "NewTable"
'
Set tdf = db.CreateTableDef(strTableName)
tdf.Connect = "ODBC;DSN="DSN01";;TABLE=" & strTableName & ""
'
tdf.SourceTableName = "ExternalTableName"
'
db.TableDefs.Append tdf
'
Set Idx = tdf.CreateIndex("PrimaryKey")
Set Fld = tdf.Fields("FldA")
Idx.Primary = True
Idx.Unique = True
* * * * Folling line Failes * * * *
Idx.Fields.Append Fld
tdf.Indexes.Append Idx
'
End Sub
Watch variable indicates 0 objects for Fields in Idx collection???
When following code executed, recieve error message: Run time error'3367':
Can't Append. An object with the name already exists in the collection.
Option Compare Database
Option Explicit
Dim db As Database
Dim tdf As TableDef
Dim strDSNName As String
Dim strTableName As String
Dim Idx As Index
Dim Fld As Field
'
Sub BuildTableLink()
Set db = CurrentDb
Set strTableName = "NewTable"
'
Set tdf = db.CreateTableDef(strTableName)
tdf.Connect = "ODBC;DSN="DSN01";;TABLE=" & strTableName & ""
'
tdf.SourceTableName = "ExternalTableName"
'
db.TableDefs.Append tdf
'
Set Idx = tdf.CreateIndex("PrimaryKey")
Set Fld = tdf.Fields("FldA")
Idx.Primary = True
Idx.Unique = True
* * * * Folling line Failes * * * *
Idx.Fields.Append Fld
tdf.Indexes.Append Idx
'
End Sub
Watch variable indicates 0 objects for Fields in Idx collection???