说我有一张A,B和A列的桌子。 C和我有以下代码来查找列名称,我想将这些名称加入像“A B C”这样的数组。但是,它返回的是下标超出范围运行时间error9。我该怎么做才能摆脱这个错误?提前谢谢!
Option Compare Database
Private Sub check_tbl2_Click()
Dim ind() As String
Dim joinInd As String
Dim l As Integer
For l = 0 To CurrentDb.TableDefs("tbl2").Fields.Count - 1
MsgBox (CurrentDb.TableDefs("tbl2").Fields(l).Name)
ind(l) = CurrentDb.TableDefs("tbl2").Fields(l).Name
Next l
joinInd = Join(ind)
MsgBox (joinInd)
End Sub
答案 0 :(得分:0)
您永远不会为ind()设置尺寸。这是实现目标的一种方法。看看redim声明。
Sub check_tbl2_Click()
Dim ind() As String
Dim joinInd As String
Dim l As Integer
ReDim ind(CurrentDb.TableDefs("tbl2").Fields.Count - 1)
For l = 0 To CurrentDb.TableDefs("tbl2").Fields.Count - 1
MsgBox (CurrentDb.TableDefs("tbl2").Fields(l).Name)
ind(l) = CurrentDb.TableDefs("tbl2").Fields(l).Name
Next l
joinInd = Join(ind)
MsgBox (joinInd)
End Sub
答案 1 :(得分:0)
这应该适合你:
编辑:实际上,汤姆柯林斯'如果你决定继续使用数组方法,答案要好得多,所以不要使用它。我没有最有效率的思考。
Private Sub check_tbl2_Click()
Dim ind() As String
Dim joinInd As String
Dim l As Integer
ReDim ind(1)
For l = 0 To CurrentDb.TableDefs("tbl2").Fields.Count - 1
'MsgBox (CurrentDb.TableDefs("tbl2").Fields(l).Name)
ReDim Preserve ind(UBound(ind) + 1)
ind(l) = CurrentDb.TableDefs("tbl2").Fields(l).Name
Next l
joinInd = Join(ind)
MsgBox (joinInd)
End Sub
但为什么不呢:
Private Sub check_tbl2_Click()
Dim joinInd As String
Dim db As DAO.Database
Dim fld As DAO.Field
Set db = CurrentDb()
For Each fld In db.TableDefs("tbl2").Fields
joinInd = joinInd & fld.Name
Next
MsgBox (joinInd)
Set db = Nothing
End Sub