我想知道是否有人可以帮助我。我试图从SQL表中返回的记录集中填充公用字典。从RecordSet填充字典
这一切似乎工作正常,除了一部分。我不知道它是否不正确地计算字典中的键数,或者它们没有被正确输入,但是无论出于何种原因,当我尝试显示总计数时,它总是只返回1(应该是大约15),只显示第一行的详细信息。
任何人都可以帮忙吗?
Public UserList As New scripting.Dictionary
Sub UserDL()
Dim USList As Range
Dim USArr(0 To 11) As Variant
Call ConnecttoDB
Set Cmd = New adodb.Command: Set rs = New adodb.Recordset
With Cmd
.CommandTimeout = 30
.ActiveConnection = cn
.CommandText = "CSLL.DLUsers"
Set rs = .Execute
End With
With rs
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
While (Not .EOF)
For i = 1 To 11
USArr(i - 1) = rs(i)
Next i
With UserList
If Not .Exists(rs("Alias")) Then
.Add Key:=rs("Alias"), Item:=USArr
End If
End With
.MoveNext
Wend
End If
End With
IA = UserList.Items
Debug.Print UserList.Count & " Items in the dictionary"
For Each element In IA
For i = 0 To 10
Debug.Print element(i)
Next i
Next element
Set Cmd = Nothing: Set rs = Nothing ': Set UserList = Nothing
End Sub
真讨厌 - 完美工作第一次尝试。非常感谢!我花了几个小时。出于好奇,首先将其作为变量声明的原因? – Tom 2014-09-04 16:57:43
如果没有数据类型,字典会存储一个指向内存位置的指针,而不是内存本身。所以所有的迭代指向相同的位置。 – cyboashu 2014-09-05 08:58:43