vuyy1182
vuyy1182

Reputation: 1686

How to store table column names into array using access vba

I am pretty new to VBA . I have a table contains more than 30 fields. How do i store the column names into array using vba

My vba Code

Dim tdf As TableDef
Dim fld As Field
Dim o As Integer

Set tdf = db.TableDefs(tablename)
Dim n As Integer
n = tdf.Fields.Count
ReDim tablecolumns(0 To n) As String

For o = 0 To n
    tablecolumns(n) = fld.Name(o)
Next o

Giving an error in this line

  tablecolumns(n) = fld.Name(o)

Upvotes: 0

Views: 6075

Answers (1)

Blackhawk
Blackhawk

Reputation: 6140

Ah, I'm modifying my answer to match your code.

Dim tdf As TableDef
Dim fld As Field
Dim o As Integer

Set tdf = db.TableDefs(tablename)
Dim n As Integer
n = tdf.Fields.Count
Dim tablecolumns() As String
ReDim tablecolumns(0 To n-1)

For o = 0 To n-1
    tablecolumns(o) = tdf.Fields(o).Name
Next o

Upvotes: 1

Related Questions