Fill Dictionary object

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Fill Dictionary object

Post by sal21 »

Why the code not intercept a duplicates Key????? and how to iterate from the DICT element when the code have filled the Dictionary object?:

Code: Select all

Option Explicit
Private Sub test()

Dim DICT As Object
Dim Con As ADODB.Connection
Dim Cmd As New ADODB.Command
Dim Rs As ADODB.Recordset
Dim Rs1 As ADODB.Recordset
Dim SQL As String, RIGA As Long
Set DICT = New Dictionary

RIGA = 0
Set Con = New ADODB.Connection
Con.Open "provider=microsoft.jet.oledb.4.0;data source=C:\ASS_MF\BT\MAT.mdb"

Set Rs = New ADODB.Recordset
SQL = "SELECT ASS_MF_EC.RAPPORTO FROM ASS_MF_EC ORDER BY ASS_MF_EC.RAPPORTO"
Set Rs = Con.Execute(SQL) 
While Not Rs.EOF
If DICT.Exists(Rs.Fields(0).Value) Then
MsgBox "already exists!"
Else
RIGA = RIGA + 1
DICT.Add RIGA, Rs.Fields(0).Value
End If
Rs.MoveNext
Wend

Set Rs = Nothing
Con.Close
Set Con = Nothing

End Sub


User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Fill Dictionary object

Post by HansV »

Exists checks whether the key already occurs. But you set RIGA as key, not Rs.Fields(0).Value...
It is unlikely that Rs.Fields(0).Value matches one of the values of RIGA.

But I don't understand why you want to do it this way. If you use

SQL = "SELECT ASS_MF_EC.RAPPORTO FROM ASS_MF_EC GROUP BY ASS_MF_EC.RAPPORTO"

the values of RAPPORTO in the recordset are guaranteed to be unique, so you don't need a dictionary object.
Best wishes,
Hans