Adding a table column using VBA in Access

Hello,

I was wondering if it were possible at all to add a column to an already existing table using VBA code. It seems like a simple enough procedure, but I can't seem to figure out how to do it. I'm beginning to wonder if it's even possible.

Any help would be appreciated, thanks!

CJ



Answer this question

Adding a table column using VBA in Access

  • Kagan A

    I've actually solved the problem. It was pretty simple, like I figured it would be. I'll go ahead and post the code in case anyone else happens to be new to this and runs across the same problem in the future.

    Public Sub addColumn()
    Dim strField As String
    Dim curDatabase As Object
    Dim tblTest As Object
    Dim fldNew As Object

    Set curDatabase = CurrentDb
    Set tblTest = curDatabase.TableDefs("Test")

    strField = "TestColumn"

    Set fldNew = tblTest.CreateField(strField, dbLong)
    tblTest.Fields.Append fldNew
    End Sub

    I actually had all of this correct except I was putting parentheses around fldNew in the Append statement.

    Thanks again to anyone who reads this!


  • Adding a table column using VBA in Access