CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012

    Adding a New Field to an existing Access Database in the code window

    Hi Pals,
    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

  2. #2
    Join Date
    Jul 2006

    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
    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]
    End Function
    yourConnectionObject is your DB connection, and you ought to substitude YourTableName and the FieldName.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.