我想知道是否有人可以幫助我。我試圖從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