I have a need to change an autonumber field in a table to a long integer type then to add another autonumber field as a key field. I can't find a way to change the field type. I can delete the field and add a new one which works great but I need to change the existing field properties first.
The following code is based on some sample code that I have modified to do most of what I need but have just got a little stuck on some things.
Sub ModifyTable()
Dim dbsThisDatabase As Database
Dim tdfThisTable As TableDef, fld1 As Field, fld2 As Field
Dim idx As Index, fldIndex As Field
Set dbsThisDatabase = CurrentDb
Set tdfThisTable = dbsThisDatabase.TableDefs!tblPresortedMasterFile
Set fld1 = tdfThisTable.CreateField("ContactID", dbLong)
fld1.Attributes = fld1.Attributes + dbAutoIncrField
tdfThisTable.Fields.Append fld1
' Create primary key index.
Set idx = tdfThisTable.CreateIndex("PrimaryKey"
Set fldIndex = idx.CreateField("ContactID", dbLong)
' Append index fields.
idx.Fields.Append fldIndex
' Set Primary property.
idx.Primary = True
' Append index.
tdfThisTable.Indexes.Append idx
dbsThisDatabase.TableDefs.Refresh
Set dbsThisDatabase = Nothing
End Sub
The following code is based on some sample code that I have modified to do most of what I need but have just got a little stuck on some things.
Sub ModifyTable()
Dim dbsThisDatabase As Database
Dim tdfThisTable As TableDef, fld1 As Field, fld2 As Field
Dim idx As Index, fldIndex As Field
Set dbsThisDatabase = CurrentDb
Set tdfThisTable = dbsThisDatabase.TableDefs!tblPresortedMasterFile
Set fld1 = tdfThisTable.CreateField("ContactID", dbLong)
fld1.Attributes = fld1.Attributes + dbAutoIncrField
tdfThisTable.Fields.Append fld1
' Create primary key index.
Set idx = tdfThisTable.CreateIndex("PrimaryKey"
Set fldIndex = idx.CreateField("ContactID", dbLong)
' Append index fields.
idx.Fields.Append fldIndex
' Set Primary property.
idx.Primary = True
' Append index.
tdfThisTable.Indexes.Append idx
dbsThisDatabase.TableDefs.Refresh
Set dbsThisDatabase = Nothing
End Sub