June 21st, 2012, 04:38 AM
Adding a New Field to an existing Access Database in the code window
Please am new in this forum and need a little help with an access database program. What code will I use to create another field in an existing access database from my visual basic code window automatically when a user of my application wants to append a field to the database. Thanks
June 21st, 2012, 11:13 AM
Re: Adding a New Field to an existing Access Database in the code window
To access the structures of tables you have to make use of the ADO Extension (ADOX) library.
Go to Project -> References and add "Microsoft ADO Ext. 2.8 for DDL and security" to your references.
This makes available the extended ADO objects. To add a field (column) to an existing table proceed as follows
yourConnectionObject is your DB connection, and you ought to substitude YourTableName and the FieldName.
Public Function AddColumn()
Dim cat As New ADOX.Catalog
Dim tbl As ADOX.Table
Set cat.ActiveConnection = yourConnectionObject
cat.Tables("YourTableName").Columns.Append "FieldName", [Type], [Size]
The items in square brackets ore optional and depend on the field type you want to create. (You do not write the brackets).
If you want to create an integer field you go ...Append "myIntField", adInteger
For a string filed you'd want to specify the length of 50 characters like ...Append "myStringField", adVarWChar, 50
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.