如何使用access vba将表列名存储到数组中

时间:2014-04-24 21:09:06

标签: vba access-vba

我对VBA很新。我有一个包含30多个字段的表。如何使用vba

将列名存储到数组中

我的vba代码

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

在此行中发出错误

  tablecolumns(n) = fld.Name(o)

1 个答案:

答案 0 :(得分:1)

啊,我正在修改我的答案以匹配您的代码。

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