Ms access 将数组分配给集合

Ms access 将数组分配给集合,ms-access,vba,Ms Access,Vba,从代码中很容易看出我想要什么,但到目前为止没有任何效果: Sub test() Dim C As New Collection Dim A() As String ReDim A(0, 1) A(0, 0) = "row 0, col 0" A(0, 1) = "row 0, col 1" C.Add A(0), "first" ' subscript out of range error Debug.Print C.Item("fi

从代码中很容易看出我想要什么,但到目前为止没有任何效果:

Sub test()
    Dim C As New Collection
    Dim A() As String

    ReDim A(0, 1)
    A(0, 0) = "row 0, col 0"
    A(0, 1) = "row 0, col 1"

    C.Add A(0), "first" ' subscript out of range error

    Debug.Print C.Item("first")(0) & ", " & C.Item("first")(1)
End Sub
所以,我只想将数组作为集合的成员


非常感谢您的帮助。

您要的不是收藏,而是词典

  • Microsoft脚本运行时
    添加到您的参考(工具/参考)

  • 使用字典而不是集合稍微修改代码:

    Sub test()
       Dim C As New Scripting.Dictionary '<-- not collection
       Dim A() As String
    
       ReDim A(0, 1)
       A(0, 0) = "row 0, col 0"
       A(0, 1) = "row 0, col 1"
    
       C.Add "first", A '<-- key first, item then
    
       Debug.Print C("first")(0, 0) & ", " & C("first")(0,1) <-- you need to access the elements properly (bi-dimensional)
    
    End Sub
    
    子测试()
    
    Dim C作为一个新的脚本。Dictionary“你不是在寻找一个集合,而是一个字典。”你为什么这么说?你回答的第二部分是解决方案,但我对第一部分很好奇。为什么不使用集合呢。