Microsoft Access VBA Macro Programming

(Tina Sui) #1

Chapter 31 Create and Change Table Structures


Chapter 31 Create and Change Table Structures


A


s your Access application is running, there may be a need to create a table
on-the-fly or amend a table structure. For example, you may be importing data
into a table from a text file. If for any reason the data in the field in the text file is
too large for the field in your table, it will be truncated and data will be lost.
You may also wish to create a table for a specific operation, do that operation, and then
delete the table.
You can also change the properties of each individual field, such as whether it is a
required field or whether it is indexed.


Creating a Table


Use this VBA code to create a simple table:


Sub CreateTable()
Dim MyTable As TableDef


Set MyTable = CurrentDb.CreateTableDef("NewTable")


MyTable.Fields.Append MyTable.CreateField("TextField", dbText, 100 )
MyTable.Fields.Append MyTable.CreateField("LongIntegerField", dbLong)
MyTable.Fields.Append MyTable.CreateField("MemoField", dbMemo)
MyTable.Fields.Append MyTable.CreateField("YesNoField", dbBoolean)
MyTable.Fields.Append MyTable.CreateField("DateTimeField", dbDate)


313

Free download pdf