
Adding a new field to an existing database
Brian, it's been a few years since I've done this in code and Jet has
changed a bit. But if nothing else, this is a good start for you:
1. Dim a variable as a field type.
2. Open your database into a database object.
3. Open your table (that you want to add the field to) into a tabledef
object.
4. Use the CreateField method of the tabledef object to create the field in
the field variable.
5. Use the TableDef.Fields.Append method to add the field to the table.
Here is a code snippet for you:
Private Sub Create_New_Field()
Dim dbDatabase As Database
Dim tblBooks As TableDef
Dim fldSubtitle As Field
Set dbDatabase = DBEngine.OpenDatabase(strPath & "reference.mdb")
Set tblBooks = dbDatabase.TableDefs("Books")
Set fldSubtitle = tblBooks.CreateField("Subtitle", dbText, 25)
'Set any field properties here. Most properties can't be changed
'once the field is added to the table.
fldSubtitle.AllowZeroLength = True
tblBooks.Fields.Append fldSubtitle
Set fldSubtitle = Nothing
Set tblBooks = Nothing
Set dbDatabase = Nothing
End Sub
I didn't try to run that code but if memory serves me, it should work. If
you have any questions regarding the arguments to the CreateField method,
just punch in the code and search help for the argument. dbText is a
constant in the DAO module. Other field types are noted in help with the
constant to use for each one.
Hope this helps.
Mike
Quote:
> Is it possible to add a new field to an existing database? If so, how?
> Or, is my only option to create a new database with the new field and
> copy all the info from the old db to the new one?