Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dictionaries - Creating / Retrieving Data 1

Status
Not open for further replies.

fabby1

Technical User
Mar 9, 2004
206
GB
HI

I have created a dictionary with the help of you guys. But I am still a little confused as how these things really hang together. I have created a dictionary fine. The data I have contains just a name and a value

e.g.
"Name" "Total"
IA/R1 3434
IA/R1 4343

I have used the following to create this

Code:
Public Function Create_Dictionary_dLine5()
Dim dbsCurrent As Database

Set dbsCurrent = CurrentDb
Set dLine5 = New Scripting.Dictionary
Set rstLine5 = dbsCurrent.OpenRecordset( _
              "SELECT IDATE, DISPLAYNAME, Sum(TAGVALUE) AS Total, ID " _
            & "FROM TMP_Daily_Table " _
            & "WHERE IDATE LIKE #" & G_DATE & "# AND DISPLAYNAME Like ""To*"" Or DISPLAYNAME Like ""Lev*""" _
            & "GROUP BY IDATE, DISPLAYNAME, ID ", dbOpenDynaset)
With rstLine5
   .MoveLast
   .MoveFirst
  Do While Not .EOF
    Tmp_Name = !DISPLAYNAME
    Tmp_Val = !Total
    dLine5.Add Tmp_Name, Tmp_Val
    .MoveNext
  Loop
End With

and use this to retrieve the data into a form I use

Code:
   For Each strKey In dLine5.Keys
    Forms!DA_DAILY_Summary.Controls(strKey).Value = dLine5.Item(strKey)
   Next

??? Does this create 1 dictionary, with the values are inside.

e.g.
-dLine5----------
| |
| IA/R1 3434 |
| IA/R2 4343 |
| ....... |
-----------------

or a dictionary for each value e.g. IA/R1 & IA/R2 ....

?????????????????

I have now created another dictionary where I want to store 2 values relating to diebacks

e.g.
"var1" "Duration" "Total"
1A-D1 4235 33
1A-D2 3323 23
1B-D1 33 53

so I have created the following
Code:
Public Function Create_Dictionary_dDieBacks()

Dim dbsCurrent As Database

Set dbsCurrent = CurrentDb
Set dDiebacks = New Scripting.Dictionary
Set dAttrList = New Scripting.Dictionary
'===============================================================================
'  Create Recordset For DIEBACKS and Populate DICTIONARY
'===============================================================================
Set rstDIE = dbsCurrent.OpenRecordset( _
        " SELECT Sum(dur) AS Duration, msg, var1, Count(msg) AS Total " _
      & "FROM L123_ALARMS " _
      & "GROUP BY msg, var1, CDate(Format([itime],""dd/mm/yyyy""))" _
      & "HAVING (((msg) Like ""*In Dieback*"") AND ((CDate(Format([itime],""dd/mm/yyyy""))) Like #4/20/2004#))", dbOpenDynaset)
With rstDIE
  .MoveLast
  .MoveFirst
Do While Not .EOF
  dKey = .Fields("var1")
  dDiebacks.Add dKey, dAttrList
  dDiebacks(dKey)("Duration") = .Fields("Duration")
  dDiebacks(dKey)("Total") = .Fields("Total")
 .MoveNext
Loop
End With

How do I retrieve the data from the dictionary ??
and how is it stored ??

I know I'm asking alot but would appreciate any help in visualizing how they are created.

Thanks

Phil



 
A dictionary is an associative array, a set of name/value pairs. You access the values either through a numeric subscript (standard arry type entry), or by its name, that name that is associated with that value. (much like a collection)

A simple dictionary, in your first example, is just a set of name/value pairs:

Item1: Name/Value ==> IA/R1 / 3434
Item2: Name/Value ==> IA/R1 / 4343

The value part of a dictionary can be of any type, including object, and may be any type of object, including another dictionary. Your second example does that. You first create three simple dictionaries each which contain two name/value pair entries.
[tt]
SubDirOne SubDirTwo SubDirThree
Item: Name/Value Item: Name/Value Item: Name/Value
1 : Dur/4236 1 : Dur/3323 1 : Dur/33
2 Tot 33 2 : Tot/23 2 : Tot/53
[/tt]
Holding these is your outer dictionary which contains 3 Name/Value pairs. Here the Value is another Dictionary object:
[tt]
Item: Name / Value
1 1A-D1 / SubDir1
2 1A-D2 / SubDir2
3 1B-D1 / SubDir3
[/tt]
Retrieving the Value is by referencing the keys, or the numeric subscript value.
[tt]
x = dDiebacks("A1-D2")("Total")
---------------------- ==> Returns SubDir2
dAttrList("Total") Returns 23
x = 23
[/tt]

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Caj

You are brilliant, that has helped me alot,

just to finish off,

If I want to retrieve the data from the subdir, how do I go about iterating through the look to retrieve it..

I have had a god

Code:
   For Each ttrKey In dDiebacks.Keys
      For Each strKey In dAttrList(ttrKey).Keys
        t_Name = dDiebacks(ttrKey).Item
        t_Duration = dDiebacks.Item(strKey)
        t_Total = dDiebacks(dKey).Item("Total")
      Next
   Next

Many Many Thanks

Phil
 
Caj

You are brilliant, that has helped me alot,

just to finish off,

If I want to retrieve the data from the subdir, how do I go about iterating through the look to retrieve it..

I have had a go

Code:
   For Each ttrKey In dDiebacks.Keys
      For Each strKey In dAttrList(ttrKey).Keys
        t_Name = dDiebacks(ttrKey).Item
        t_Duration = dDiebacks.Item(strKey)
        t_Total = dDiebacks(dKey).Item("Total")
      Next
   Next

Many Many Thanks

Phil
 
Caj

Can you see where I'm going wrong in trying to retrieve the data from a dictionary in a dictionary

Thanks

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top