Kato
Kato

Reputation: 51

Reordering Columns with OrdinalPosition in Access table

I have an imported Excel spreadsheet and have added new fields to the end of the table. I know it is possible to simply re-order the fields manually, but I'd like to automate it for every import.

This is the code I have been playing with, but the table does not update with the defined OrdinalPosition for the column I am trying to move. I want to later apply this same technique for multiple columns.

Sub ReorderColumns()
    Dim db As Database
    Dim td As TableDef
    Dim NewField As Field

    Set db = CurrentDb
    Set td = db.TableDefs("Imported Pcard")

    td.Fields("Postal Code").OrdinalPosition = 2

    td.Fields.Refresh
End Sub

Upvotes: 1

Views: 1636

Answers (1)

June7
June7

Reputation: 21370

Explicitly declare db and td as DAO objects.

    Dim db As DAO.Database
    Dim td As DAO.TableDef

Refresh method is not needed.

Upvotes: 2

Related Questions