Adding New Fields to an Existing Table

At times you may want to programmatically add a new field to an existing table. The procedure below adds a new text field called MyNewField to a table located in the current database. The new field can hold 15 characters. For more information on adding fields to a table, refer to the section called "Creating a Microsoft Access Table" at the beginning of this chapter.

©Hands-On 11-4 : Adding a New Field to a Table

The procedure in this hands-on references the tblFilters table in the Acc2003_Chap11 database. Since we deleted this table in the previous hands-on, you need to rerun the procedure in Hands-On 11-1 to recreate the table.

1. In the Visual Basic Editor window, choose Insert | Module.

2. In the module's Code window, type the Add_NewFields procedure shown below.

3. Choose Run | Run Sub/UserForm to execute the procedure.

Sub Add_NewFields()

Dim cat As New ADOX.Catalog Dim myTbl As New ADOX.Table

Set cat = New ADOX.Catalog cat.ActiveConnection = CurrentProject.Connection cat.Tables("tblFilters").Columns.Append _

"MyNewField", adWChar, 15 Set cat = Nothing End Sub

Creating and Manipulating Databases with ADO

0 0

Post a comment