Home

DAO: Adding a New Column to an Existing Table

 

Description

To programmatically add a new column, declare a variable of type DAO.Field. 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 DAO.Database
    Dim tblCustomers As DAO.TableDef
    Dim colFullName As DAO.Field

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

 

     
 

Home Copyright © 2009-2016, FunctionX, Inc.