How to create a variable with the name of a variables content?

DKDiveDude

Registered User.
Local time
Today, 13:52
Joined
Mar 28, 2003
Messages
56
Is there any way to to create a variable with the name of another variables content in VBA?

PHP has something like what I now need in VBA, I believe it is something like this:

// Put text string in a variable
$Fruit="Apple";

// Now create a variable named as the content of the above variable and put content in the new variable
$$Fruit=5;

// Print the content of $Apple which should now contan 5
echo $Apple;


Of course to be of real value the newly created variable should be available globally, another thing I don't know if MS Accces can do, unless defined in a module.

Alternatively I would need something like being able to define an array of names and then assign a number to each name, like this:

Dim Fruit
Fruit = Array("Apples", "Bananas", "Oranges")

and then later do this:

Fruit("Apples")=5
 
DKDiveDude,

Interesting question! I'd sure like to know if there is a way to do that.

I don't know of a way, but I like as to your idea of an array to do it. You would have to know which array element represents a particular fruit.

Or just use a multidimensional array?
Code:
Dim MyFruits(1 To 3, 1 To 2) As Variant

MyFruits(1, 1) = "Apples"
MyFruits(2, 1) = "Bananas"
MyFruits(3, 1) = "Oranges"

MyFruits(1, 2) = 5    'Number of Apples
MyFruits(2, 2) = 10    'Number of Bananas
MyFruits(3, 2) = 15    'Number of Oranges

Dim i As Long
For i = LBound(MyFruits, 1) To UBound(MyFruits, 1)
    Debug.Print "Number of '" & MyFruits(i, 1) & "' = " & MyFruits(i, 2)
Next i
 
Last edited:
Sure but then I have to search through the multidimensional array to find the fruit for which I want to change the value.
 
Is there any way to to create a variable with the name of another variables content in VBA?

Yes.

Dim str as string
str = "sdfkjhsdfjhbsdfkSDFLKsnf,SDKF OR WHATEVER"

To make str a global object, put the "dim" statement in a module and arbitrarily name it neumonically basGlobalVariables. After Access compiles your program, str will be available to all Access objects.
 
Basically, no. You can't do it the way you would in PHP. I have no idea why this is useful to you, but I suspect you are trying to do something the wrong way. Still, supposing you have your reasons, the best Access gives you is a collection object.

Dim col as New Collection

gives you a structure not too dissimilar from a PHP array. Well, quite dissimilar in many ways, but it has the freedom of allowing you to store values with items with string tags.

So if you have strFruit = "Apple" and intCount = 4 and you want to store these together, you can use:

col.Add intCount, strFruit

And

debug.print col("Apple")

Will produce 4.

(Just reread your post and it seems I've stolen an apple!)

Collections are very useful for this sort of thing. You can store any type of value or object in them, though the key must be a string. Unfortunately col("Apple") is not a variable and you can't simply add to it if you want to for any reason.

I use a wrapper class to add to the functionality of Collections all the time. Would then allow this. Ask me about it if this is a useful direction for you.

Sam.
 
Note to self: Learn to use collections...
:D
 

Users who are viewing this thread

Back
Top Bottom