我正在使用 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”处收到错误
所有这些都是独一无二的,因为我在立即窗口中获得了值。
感谢您的帮助。
最佳答案
设法解决问题...它将一个对象存储到字段“Field2”中,我想要该值,所以我更改了:
dictionary.Add rs.Fields(0), counter
至
dictionary.Add rs.Fields(0).value, counter
现在它获得了值
关于vba - 填充字典导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20239305/