【问题标题】:Populating dictionary causing error填充字典导致错误
【发布时间】:2013-11-27 10:03:34
【问题描述】:

我正在使用 VBA 用表格中的代码填充字典,所有这些代码都是唯一的。虽然当我尝试将它们添加到字典中时出现错误: '这个键已经与这个集合的一个元素相关联'

我输入的代码示例如下:

GSK611、GSK612、GSK612(但它们是带有 DELETED00、ADMIN、HISTORY 的奇数代码)

我使用的方法是:

Private Function RunContactQuery(query As String) As dictionary
    On Error GoTo Catch

    Dim ex As ErrEx

    Dim dictionary As dictionary
    Set dictionary = New dictionary

    Dim rs As DAO.Recordset
    Dim counter As Integer

    Set rs = CurrentDb.OpenRecordset(query)

    If Not (rs.EOF And rs.BOF) Then
        rs.MoveFirst
        counter = 1

        Do Until rs.EOF = True
            dictionary.Add rs.Fields(0), counter

            counter = counter + 1
            rs.MoveNext
        Loop
    End If

    Set RunContactQuery = dictionary

Finally:
    Exit Function
Catch:
    ErrEx(Err, cmstrModule & ".RunContactQuery").Throw
    Resume Finally
End Function

我在“dictionary.Add rs.Fields, counter”处收到错误

所有这些都是独一无二的,因为我在即时窗口中获得了值。

感谢您的帮助。

【问题讨论】:

    标签: vba dictionary


    【解决方案1】:

    设法解决了问题...它将一个对象存储到字段“Field2”中,我想要这个值,所以我改变了:

    dictionary.Add rs.Fields(0), counter
    

    dictionary.Add rs.Fields(0).value, counter
    

    它现在得到了值

    【讨论】:

      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2021-09-18
      • 2018-01-20
      • 1970-01-01
      相关资源
      最近更新 更多