DAO: Deleting a Column From a Table



To delete a column using DAO, call the Delete() method of the TableDef object and pass it the name of the column. The syntax of this method is:

TableDef.Fields.Delete ColumnName

In this formula, replace ColumnName with the name of the column you want to delete. Here is an example:

Private Sub cmdModifyPersons_Click()
    Dim curDatabase As DAO.Database
    Dim tblPersons As DAO.TableDef

    ' Get a reference to the current database
    Set curDatabase = CurrentDb
    ' Get a reference to a table named Persons
    Set tblPersons = curDatabase.TableDefs("Persons")
    tblPersons.Fields.Delete "EmailAddress"
End Sub

Before deleting a column, make sure it exists, otherwise, you would receive a 3265 error:

Error 3265

Even if the column exists, before deleting a column, make sure its table is closed. Otherwise, you would receive a 3211 error. You can check these issues using error handling.



Home Copyright © 2009-2016, FunctionX, Inc.