In MS Access, if you right-click on a
linked table and select Design, Access will tell you that you cannot make
design changes to a linked table. Although it's true that you can't do it from
the UI, you can
do it using VBA. . .In fact, this approach will work
even if the back-end file is an older format such as Access 97 or 2000.
These examples use the DAO object library, so we need a reference to it; Also, we qualify the Dim's just in case there is also a ref
to the
In the following example, the linked table is Table1 and we want to add a long
integer field named NewLongField. Note
that 1) use the actual table name, not an alias (if there is
one) and 2) you need exclusive access to the back-end file.
Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
Const MDB = "C:\My Documents\MyDatabase.mdb"
Set db = OpenDatabase(MDB)
Set tdf = db.TableDefs("Table1")
With tdf
Set fld = .CreateField("NewLongField", dbLong)
.Fields.Append fld
Set fld = Nothing
End With
Set tdf = Nothing
db.Close
Set db = Nothing
...when adding a date field, the "Set fld = " line will look like
this:
Set fld =
.CreateField("NewDateField", dbDate)
...a text field will also require the size, like this:
Set fld = .CreateField("NewTextField",
dbText, 50)
..boolean fields can be added like this:
Set fld =
.CreateField("NewBooleanField", dbBoolean)
however this will display as -1 (True) or 0 (False). If you want to have the
customary checkbox display, here is the complete code for that:
Dim db As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
Dim tdf As DAO.TableDef
Const MDB = "C:\My Documents\MyDatabase.mdb"
Set db = OpenDatabase(MDB)
Set tdf = db.TableDefs("Table1")
With tdf
Set fld = .CreateField("NewBooleanField", dbBoolean)
fld.DefaultValue = 0
.Fields.Append fld
Set prp = .CreateProperty("DisplayControl", dbInteger,
acCheckBox)
fld.Properties.Append prp
Set prp = .CreateProperty("Format", dbText,
"Yes/No")
fld.Properties.Append prp
Set prp = Nothing
Set fld = Nothing
End With
Set tdf = Nothing
db.Close
Set db = Nothing