找不到嵌套字典对象的问题

时间:2019-06-14 15:15:04

标签: vba dictionary ms-access access-vba

我试图在MS Access VBA中创建一个子程序,其最终目标是获取一些表,要分组的字段列表以及一个数字字段作为输入,并生成一个包含分组字段的输出表和数字字段的中位数。

我要对单个字段进行分组,但是我遇到了使它在多个字段上起作用的问题。也就是说,我的问题是我正在使用嵌套字典来最终汇总分组字段值及其中位数,但是当我尝试访问内部字典时,出现“ 424运行时错误:需要对象”,我无法找出问题所在。一旦将内部词典放入外部词典中,我将无法像怀疑的那样访问它。

到目前为止,这是我的代码: `

Public Sub MedianByGroups(ByVal inputTable As String, ByVal strField As String, _
ByVal strGroup As String, ByVal outputTable As String, _
Optional ByVal strCriteria As String)

    DoCmd.SetWarnings False
    Dim db As DAO.Database: Set db = CurrentDb()
    Dim rstDomain As DAO.Recordset
    Dim strSQL As String
    Dim varMedian As Long: varMedian = 0
    Dim intRecords As Long
    Dim splitgroup As Variant

    splitgroup = Split(strGroup, ", ")

    strSQL = "SELECT DISTINCT " & strGroup & " INTO TMP_GROUPINGTABLE FROM " & _
    inputTable & ";"

    DoCmd.RunSQL (strSQL)

    Set rstDomain = db.OpenRecordset("TMP_GROUPINGTABLE")

    Dim i As Integer: i = 0

    Dim group_dict As New Dictionary
    Dim tmp_dict As Dictionary
    Set tmp_dict = New Dictionary

    If Not (rstDomain.EOF And rstDomain.BOF) Then
        rstDomain.MoveFirst
        Do Until rstDomain.EOF = True
            For Each Label In splitgroup
                tmp_dict.Add Label, rstDomain.Fields(Label).Value
            Next Label

            group_dict.Add i, tmp_dct
            Set tmp_dict = New Dictionary
            i = i + 1
            rstDomain.MoveNext
        Loop
    End If

    rstDomain.Close

    For Each key In group_dict.Keys
        For Each addlKey In group_dict(key).Keys
            Debug.Print addlKey, group_dict(key)(addlKey)
        Next addlKey
    Next key

    DoCmd.SetWarnings True
end sub

`

如果一个表看起来像这样: GROUP1 GROUP2 VAL1 A C 400 B D 500 它被称为“ MYTABLE” 可以这样称呼此子:call MedianByGroups(“ MYTABLE”,“ VAL1',” GROUP1,GROUP2“,” OUTPUTTABLE“) 并且请注意,这还没有做中位数或其他任何部分,因为我用字典碰到了这个绊脚石。一旦可以访问这些内部词典,就应该掌握所有其他内容,但是众所周知,MS ACCESS错误非常模糊且无济于事。

失败的地方是内部for循环,该循环位于靠近末尾的for循环中。 For Each addlKey In group_dict(key).Keys

1 个答案:

答案 0 :(得分:2)

一个不完整的答案 -我建立了一个没有MS Access特定部分的示例,您代码中的逻辑似乎运行良好。据我所知,下面的逻辑和上面的代码没有真正的区别。这个例子适合您吗?

Option Explicit

Sub TestingNestedDictionaries()
    Dim group_dict As New Dictionary
    Dim tmp_dict As Dictionary
    Set tmp_dict = New Dictionary

    Dim labels As Variant
    labels = Array("red", "orange", "yellow", "green", "blue", "indigo", "violet")

    Dim i As Long
    For i = 1 To 5
        Dim label As Variant
        For Each label In labels
            tmp_dict.Add label, label & "-" & i
        Next label
        group_dict.Add i, tmp_dict
        Set tmp_dict = New Dictionary
    Next i

    '--- pull out one color
'    Const THIS_COLOR As String = "green"
'    Dim group As Variant
'    For Each group In group_dict
'        Dim subDict As Dictionary
'        Set subDict = group_dict(group)
'        Debug.Print subDict(THIS_COLOR)
'    Next group

    Dim key As Variant
    Dim addlKey As Variant
    For Each key In group_dict.Keys
        For Each addlKey In group_dict(key).Keys
            Debug.Print addlKey, group_dict(key)(addlKey)
        Next addlKey
    Next key

End Sub