Hello group,
I have an application that runs Access 2007 (runtime package) at remote locations. I want to add several tables to these existing databases. The code below creates a table and adds the primary key field. When I run the code it creates an entry in the table list, but when I double-click or right-click (Design view) a message tells me it can't find the table.
thanks for your help.
I have an application that runs Access 2007 (runtime package) at remote locations. I want to add several tables to these existing databases. The code below creates a table and adds the primary key field. When I run the code it creates an entry in the table list, but when I double-click or right-click (Design view) a message tells me it can't find the table.
thanks for your help.
Code:
Private Sub CreateTable(db As DAO.Database, TableName As String, PrimaryKeyFieldName As String)
On Error GoTo ErrHandler
'Purpose: Create a table using DAO.
Dim tdf As DAO.TableDef
Dim Fld As DAO.Field
Dim Ind As DAO.index
Set tdf = db.CreateTableDef(TableName)
With tdf
Set Fld = .CreateField(PrimaryKeyFieldName, dbLong)
Fld.Attributes = dbAutoIncrField + dbFixedField
.Fields.Append Fld
End With
'Primary key index.
Set Ind = tdf.CreateIndex("PrimaryKey")
With Ind
.Fields.Append .CreateField(PrimaryKeyFieldName)
.Unique = True
.Primary = True
End With
tdf.Indexes.Append Ind
'Save the table.
db.TableDefs.Append tdf
ExitHandler:
Set Fld = Nothing
Set tdf = Nothing
Set Ind = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & " Message Number - " & Err.Number
Resume ExitHandler
End Sub