VB6词典此键已经与该集合的元素相关联 [英] VB6 Dictionary This key is already associated with an element of this collection

查看:725
本文介绍了VB6词典此键已经与该集合的元素相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来有些怪异,但是在这里:

This may sound a little weird but here it goes:

我的表单遍历了每个客户(+ 10000),并打开了一个记录集以进行最后一次访问.由于优化效果不佳,我决定执行一个查询,并且需要存储它以便于访问.

My form iterated through each customer (+-10 000) and opened a recordset to get the last visit. Since it is poorly optimized I decided to do a single query, and I need to store it for easy access.

我决定带一本字典,因为与数组不同,我将能够使用键字符串(客户编号)进行搜索并获取我的商品(字符串:客户最后一次访问).

I decided to take a dictionary because, unlike arrays, I will be able to search with a key string (the client number) and get my item (string: client's last visit).

Dim dict As Dictionary
Set dict = CreateObject("Scripting.Dictionary")


dict.RemoveAll
dict.CompareMode = TextCompare    ' BinaryCompare does not change anything
rsLastVisits.Open "SELECT CUST_NO, MAX(DATE) AS LAST FROM [...] GROUP BY CUST_NO", DBHandle, adOpenDynamic, adLockOptimistic
Do While Not rsLastVisits.EOF
   dict.Add rsLastVisits!CUST_NO, rsLastVisits!LAST
   rsLastVisits.MoveNext
Loop
rsLastVisits.Close

我收到运行时错误此键已经与此集合的元素关联".

I get the runtime error "This key is already associated with an element of this collection".

这是有趣的部分的开始,因为我的查询未返回重复项(我已仔细检查).

This is where the fun part starts, because my query does not return duplicates (I double checked).

调试时,这些是添加的值:

When Debugging, these are the values added:

第一次迭代:"0000000","2012/05/27"

First Iteration : "0000000.", "2012/05/27"

第二个迭代:"00000001","2011/06/14" 错误试图插入此行.

Second Iteration : "00000001", "2011/06/14" ERROR trying to insert this line.

在即时窗口中,如果我写

In the immediate window, If i write

dict.add "00000001", "2011/06/14"

添加它没有任何问题.

如果我清除了字典(dict.removeall),它将输入一项(无论什么要求都没有),而下一项将始终失败.如果我在即时窗口中手动输入(无变量),它将起作用.

If I clear the dictionary (dict.removeall), it will enter one item (no matter what cust. no), and the next one will always fail. If I type it by hand (no variables) in the immediate window, it works.

你们有个主意吗?谢谢!

Do any of you have an idea ? Thanks !

推荐答案

您的示例应该可以正常工作.我能想到的唯一问题是CUST_NO不是字符串.试试这个,检查是否有区别:

Your example should work fine. The only issue I can think about is if CUST_NO is not a string. Try this and check if it makes a difference:

dict.Add CSTR(rsLastVisits!CUST_NO), CSTR(rsLastVisits!LAST)

您可以发布CUST_NO的数据库定义吗?

Can you post the DB definition of CUST_NO ?

这篇关于VB6词典此键已经与该集合的元素相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆