CreateTableDef Method Example (MDB)

The following example creates a new TableDef object and appends it to the TableDefs collection of the current database.

Sub NewTable()
    Dim dbs As Database, tdf As TableDef, fld As Field
    
    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return TableDef object variable that points to new table.
    Set tdf = dbs.CreateTableDef("Contacts")
    ' Define new field in table.
    Set fld = tdf.CreateField("ContactName", dbText, 40)
    ' Append Field object to Fields collection of TableDef object.
    tdf.Fields.Append fld
    tdf.Fields.Refresh
    ' Append TableDef object to TableDefs collection of database.
    dbs.TableDefs.Append tdf
    dbs.TableDefs.Refresh
    Set dbs = Nothing
End Sub