Creating a Table and Its Fields

Tables are created using the CreateTableDef method of a Database object. The full syntax of this method is:

Set TableDefVar = DatabaseVar.CreateTableDef _
([TableDefName[, attributes[, source[, connect]]]])

where:

  • TableDefName is a string or string variable holding the name of the new TableDef object.

  • For information about the optional parameters, see the Access Help system.

Notes

  • The new TableDef object must be appended to the TableDefs collection using the Append method. However, before appending, the table must have at least one field.

  • CreateTableDef does not check for an already used TableDefName. If TableDefName does refer to an object already in the TableDefs collection, an error will occur when you use the Append method, but not before.

  • To remove a TableDef object from a TableDefs collection, use the Delete method.

Fields are created for a table using the CreateField method of the TableDef object. The syntax is:

Set FieldVar =TableDefVar.CreateField _
([FieldName[, type [, size]]])

where:

  • FieldName is a string or string variable that names the new Field object.

  • type is an integer constant that determines the data type of the new Field object. (See Table 15.1.)

  • size is an integer between 1 and 255 that indicates the maximum size, in bytes, for a text field. This argument is ignored for other types of fields.

Note

To remove a field from a TableDef object, use the Delete method.

Table 15-1. Constants for the Type Property

Data Type

Constant

Numerical ...

Get Access Database Design and Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.