Consulting

Results 1 to 6 of 6

Thread: Solved: Setting up a global dictionary

  1. #1

    Question Solved: Setting up a global dictionary

    Hi everyone, first post here.

    I could use a bit of help with the dictionary object. I am trying to write two functions, one that essentially places and order and one that receives an order (this is an inventory simulation macro). My idea is to just have a dictionary called orders and placing an order will just do
    [vba]orders.Add [arrival-time], [quantity][/vba]
    and on the receiving end it will (basically) look at the same dictionary and say, "Is the current time, 't', equal to the arrival time, 'at', if so return quantity."

    The problem is I am getting all sorts of errors. At the moment I am just getting an error 1004 "Application-defined or object defined error" which I believe is coming from the Microsoft Scripting Engine. In Declarations I have
    [vba]Dim orders As New Dictionary[/vba] and then in the main loop of the simulation I have [vba]Set orders = New Dictionary[/vba] (I'm not really sure where to put that statement, if I put it in the place or get orders functions it will get called on every iteration...). Anyway I hope my question is intelligible, if you need more code or explanation about what I'm attempting ask away. Thank you for any clues you can provide on setting up a global dictionary.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use

    [vba]
    Dim orders As Object

    Set orders = CreateObject("Scripting.Dictionary")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks. That looks like it worked. I've still got other stuff to iron out but I think I've got it now!

    I was wondering if you had time if you could explain the difference between the way I was invoking it and what you suggested (i.e., how did you come up with that? )

    Thanks again!
    Last edited by dfarmernv; 09-17-2007 at 03:36 PM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Well the main thing was that you were NEWing the variable when you declared it, and then did it again by setting it. I NEVER New in the declaration, I do it in the setting.

    Also, if you use the object type (Dictionary), you have to set a reference to the type library. This is called early-binding. You may or may not have done that, I had no way of knowing, so I used a generic object type (Object), and aligned it's type library when setting it (CreateObject), this is called late-binding.
    Last edited by Bob Phillips; 09-17-2007 at 04:42 PM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks so much! I hope my request didn't come across as rude, I appreciate you sharing your insight.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not in the least.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •