Microsoft Access Object Library:

Adding a New Column to an Existing Table



To programmatically add a new column, declare a variable of type Object. After getting a reference to the table that will receive the new column, assign the CreateField() method of the table to the column's variable. Finally, call the Append() method of the Fields collection of the table and pass it the column variable. Here is an example:

Private Sub cmdAddColumn_Click()
    Dim curDatabase As Object
    Dim tblStudents As Object
    Dim colFullName As Object

    ' Get a reference to the current database
    Set curDatabase = CurrentDb
    ' Get a reference to a table named Students
    Set tblStudents = curDatabase.TableDefs("Students")
    Set colFullName = tblStudents.CreateField("FullName", DB_TEXT)
    tblStudents.Fields.Append colFullName
End Sub

Home Copyright © 2009-2015, FunctionX, Inc.