Naming an object with a variable and adding it to a collection

Posted on 2005-04-21
Last Modified: 2010-05-02
I need to:
   1.  create object,
   2.  give it a name related to a name of an existing object in an existing collection (i.e. Bounce1, Bounce2, ... and then
   3.  add the object to the existing collection

I've tried with the following:

Public Sub AddItemToDetailsCollection()
    Dim objectName As String
    objectName = "Bounce" + Str(Details.Count + 1)    ' Details is an existing collection
    Dim objectName As New TradeDetail
    Details.Add Item:=objectName
End Sub

Many many thanks.  A
Question by:AlphaSquared
    LVL 35

    Accepted Solution

    Hi AA,

    I'm assuming TradeDetail is a class or udt, does it have a Name property?  If so, you could do something like:

        Dim objectName As New TradeDetail
        objectName.Name = "Bounce" + CStr(Details.Count + 1)
        Details.Add Item:=objectName

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Are trying to add an item to your details collection with a key so you can retrieve them later using "Bounce1", "Bounce2", etc...?

    Public Sub AddItemToDetailsCollection()
        Dim keyValue As String
        keyValue = "Bounce" + Trim(Str(details.Count + 1))
        Debug.Print keyValue
        Dim obj As New Tradedetail
        details.Add obj, keyValue
        ' to get it back out...
        Dim retrievedObj As Tradedetail
        Set retrievedObj = details("Bounce1")
        ' do something with retrievedObj...
    End Sub

    Author Comment

    Brilliant!  Fast, simple, and elegant.  Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now