Adding a new field to an existing database 
Author Message
 Adding a new field to an existing database

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?


Tue, 19 Feb 2002 03:00:00 GMT  
 Adding a new field to an existing database
I'm using VB6, if it matters.
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?



Tue, 19 Feb 2002 03:00:00 GMT  
 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?



Fri, 22 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to modified existing field and add new field in existing table for Access database

2. Add New Field To Existing Database

3. Adding new fields to an existing table

4. Add New Field in existing Table (VB6)

5. Adding New Field to Existing Repot

6. Adding a new field to an existing table via ADOX

7. adding a autonumber field to an existing database

8. Adding Tables and Fields to an existing database

9. Create new field in existing table exactly like field in second table

10. How to add new records in a new database

11. Add new listbox item to an existing array

12. Crystal, add new table to existing report?

 

 
Powered by phpBB® Forum Software