Mightyginger
Programmer
Hi guys, this is going to be something trivial I've not done but I've looked over it several times and have no idea why it doesn't work. I use this sub CreateTable to create a new Table called StrTableName in database strDbName. It creates the field Date and the table fine but Date isn't a primary field for some reason and isn't indexed even though I thought I'd specified it. What am I doing wrong please?
Thanks,
Neil.
Sub CreateTable(strDbName As String, strTableName As String)
Dim db As Database
Dim tbl As TableDef
Dim Fld As DAO.Field
Dim FldIndex As DAO.Field
Dim Idx As Index
Dim strDb As String
Dim strDbTableQuery As String
Dim i As Integer
Set db = OpenDatabase(strDbName)
'Create a new table definition for a table
Set tbl = db.CreateTableDef(strTableName)
'Create a new field in table "strDbName"
Set Fld = tbl.CreateField("Date", dbDate)
'Append field to table
tbl.Fields.Append Fld
'Create primary key index.
Set Idx = tbl.CreateIndex("PrimaryKey")
Idx.Primary = True
Idx.Required = True
Idx.Unique = True
Set FldIndex = Idx.CreateField("Date")
Idx.Fields.Append FldIndex
db.TableDefs.Append tbl
'Close the database
db.Close
End Sub
Thanks,
Neil.
Sub CreateTable(strDbName As String, strTableName As String)
Dim db As Database
Dim tbl As TableDef
Dim Fld As DAO.Field
Dim FldIndex As DAO.Field
Dim Idx As Index
Dim strDb As String
Dim strDbTableQuery As String
Dim i As Integer
Set db = OpenDatabase(strDbName)
'Create a new table definition for a table
Set tbl = db.CreateTableDef(strTableName)
'Create a new field in table "strDbName"
Set Fld = tbl.CreateField("Date", dbDate)
'Append field to table
tbl.Fields.Append Fld
'Create primary key index.
Set Idx = tbl.CreateIndex("PrimaryKey")
Idx.Primary = True
Idx.Required = True
Idx.Unique = True
Set FldIndex = Idx.CreateField("Date")
Idx.Fields.Append FldIndex
db.TableDefs.Append tbl
'Close the database
db.Close
End Sub