Results 1 to 4 of 4
  1. #1
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

    Looking for sample vba ---Iterating over MS Access Dictionary

    I have searched but can not find a clear example for iterating over a dictionary object. The dictionary
    contains different object types.

    There are 3 item(s) in the dictionary/object
    DictItems(0) is a Collection called <items> with 3 element(s)
    DictItems(1) is a Dictionary called <obj> with 2 element(s)
    DictItems(2) is a String called <message>

    My goal is to iterate over the dictionary and report/process each of the elements in each of the Items.
    I'd like the solution to be sufficiently generic in that keys and values can be determined via the vba code.



    I am attaching a jpg showing the watches related to the dictionary object I am testing with.

    If you have a sample showing a vba procedure for iterating over a dictionary, or a link to same or a tutorial, or have some ideas on approach, I would appreciate any suggestions.

    Thanks in advance.

    NOTE: I have cross posted this at
    http://www.access-programmers.co.uk/...59#post1515159
    Attached Thumbnails Attached Thumbnails DictionaryWatches.jpg  
    Last edited by orange; 12-19-2016 at 06:20 PM. Reason: notice of cross post

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Have you got some sample code of the method you're using to add items to the dictionary?

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Yes, but it is quite messy. I am using the class module (clsJSONParser) from Steve Bishop's #34 youtube https://www.youtube.com/watch?v=cgmjx1C0FU4 and the associated work files.

    'The class module was downloaded from Steve Bishop's Northwind.accdb from his youtube video on VBA and JSON
    ' parse string and create JSON object (Dictionary or Collection in VB)

    I have had some feedback from the other forum, but have not had internet for the last few days --just recently restored.
    I can not see a simple way to get the names for the name in the name/value pair.
    I think you should be able to parse the JSON without knowing (in advance) the structure of the JSON string. Most solutions I've seen seem to be customized to the specific string involved.

    My current thought is to create an array via Split of the JSON string on ",". Then process the array elements --may be more trouble than it's worth, but at least I can see the "Names" and could process them. The issue then becomes the multitude of combinations for more complex JSON.

    It seems other languages have parsers to do this (my best guess since I'm not conversant with most other languages and have just seen the JSON as an issue to attack based on a few posts by people trying to get info out of a JSON string returned by Amazon , Paypal etc.)

    I should point out that I have an interest in class modules, but not a whole lot of practice or experience. And using Collections and Dictionaries is new to me.


    Here is the JSON string I was trying to parse and identify individual pieces:
    {
    "items": [
    {
    "key": "First",
    "value": 100
    },{
    "key": "Second",
    "value": false
    },{
    "key": "Last",
    "value": "Mixed"
    }
    ],
    "obj": {
    "number": 1.2345e-6,
    "enabled": true
    },
    "message": "Strings have to be in double-quotes."
    }
    Last edited by orange; 12-26-2016 at 03:41 PM. Reason: clarification

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    new to you and me both, I haven't created my own class module either. I've created global functions that I use in pretty much every database I use but that's just copying a 'globalfunctions' module to each database I make. I'll try to take a look at it over the next couple of days but I would also be interested as my early experiments on your first post had the same issue you can only reference the data if you already know the 'name' which isn't necessary. Perhaps instead of an array make two dictionaries, one containing an incrementing value (0 - x) with the 'name' of the dictionary item which you can then use to look up what you want. This is an interesting problem so if you find a solution I would be interested to see the solution. I would also like to know if you notice an improvement in performance, the stuff I've read says there should be and this might be another way to improve my own code on some time consuming things.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 08-30-2016, 06:53 AM
  2. Access Sample application
    By karthiccr in forum Tutorials
    Replies: 3
    Last Post: 10-27-2015, 05:04 PM
  3. Sample project for Access 2010
    By bubai in forum Access
    Replies: 5
    Last Post: 11-28-2014, 05:49 PM
  4. Replies: 7
    Last Post: 08-01-2013, 11:52 AM
  5. Replies: 4
    Last Post: 07-29-2013, 10:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums