create dyanamic array with string references vba

Posted on 2006-05-06
Last Modified: 2010-05-01
Hi,  I would like to create an array and reference each object within the array as a string:

for example arr("a") = 1, arr("b") = 2

where "a" and "b" are not constants.

Does anyone know how to do this?

Question by:vishalarya
    1 Comment
    LVL 85

    Accepted Solution

    This is commonly known as a HashTable where you access items by "key".

    In VB you can use the Collection or Dictionary (with a reference to the "Microsoft Scripting Runtime") objects.

    Here is a simple example...

        Dim values As New Collection
        Dim strVal As String
        Dim intVal As Integer
        strVal = "a"
        intVal = 1
        values.Add intVal, strVal
        strVal = "b"
        intVal = 2
        values.Add intVal, strVal
        MsgBox values("a")
        strVal = "b"
        Dim val As Variant
        val = values(strVal)
        MsgBox val


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    779 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

    11 Experts available now in Live!

    Get 1:1 Help Now