Value of a variable

SunWuKung

Registered User.
Local time
Today, 04:58
Joined
Jun 21, 2001
Messages
172
I have a variable name stored in a string.
How could I get the value of the variable?

E.g.
VariableName="ModelID" - what is the value of ModelID?

Thanks for the help.
SWK
 
You have stored "ModelID" as a string, it doesn't have a value.

If ModelID is a field on the active form

VariableName=me!ModelID

stores the value store of ModelID in VariableName.
 
hi,
sorry I didn't explain it clearly enough.

ModelID is a Public variable, lets say it has a value of 5.
I have VariableName="ModelID"

How could I get the value of 5 just by using VariableName.

Thanks for the help.
SWK
 
Sun,

Debug.print MyVariable

The line above prints the variable's value to the Immediate Window. And the line below displays the variable's current value in a message box.

MsgBox "The variable's value is " & MyVariable

Putting it all togther in code:
Code:
Dim MyVariable As String
myVariable = "I like Pest better than Buda"
Debug.Print MyVariable
MsgBox "The Variable is " & MyVariable


Regards,
Tim
 
If the variable you are storing is going to be a whole number then you are better off dimensioning it as an integer or long integer.

What you were doing was setting a string variable to "ModelID" which will be literally read and displayed as ModelID.

If you still wish for this number to become a string then:

VariableName = CStr(ModelID)

In view of good programming ethics it is useful to prefix your variables with an identifier so you understand the data types of each, and also to aid future programmers should they come to your database.

Prefix an integer with int
Prefix a string with str
Prefix a long integer with lng
Prefix a single with sng
Prefix a double with dbl
Prefix a date with dte
Prefix a boolean with boo
Prefix currency with cur

Also, to differentiate between variables created at different times with your database you can add a further prefix:

For a variable declared within the current procedure you don't have to bother
For a variable that is declared as public, prefix it with a p
For a variable that is declared globally, prefix it with a g

So, for a public boolean that we wish to name QuitAccess, it's a better practice to add the prefixes:

p boo QuitAccess which becomes pbooQuitAccess.

At a glance you can immediately tell more about the variable - know it's type and at what level it has been declared.

If everybody stuck to the naming conventions then decoding people's code would become so much easier - helping people on the forum when they have a problem to code wouldn't lead to so many misunderstandings, and becoming the owner of someone else's database would be instantly more understandable than one where you would have to search about to find out the data type, etc...
 
Last edited:
Ok. This is the situation:
I have a table that lists variable names. I run a query and get back a variable name.
In the code I need to get the value of the variable the name of which I get from the table.

So I get a variable name as a string and I need to get the value of the variable the name of which I have as a string. ModelID is a properly defined integer.

sorry for this slow type of explanation I hope that makes sense now

(yepp, pono - though I love both)

Thanks.
SWK
 
Wouldn't DLook up be easier?
Milo, which naming convention should we follow?
 
Rich,
I don't think I could use DLook. The value of the variable is not stored in the table just the name of the variable.
 
Sun,

You're begging the question so I have to ask: Is the universe infinite?

Kidding.

The real question: Why are you storing potential names of potential variables in a table? That is, the names in the table are not variables -- they are values in a table -- text values that, if I understand this correctly, you want to dynamically retrieve and use as a model to declare a variable and then, once that variable has been created, fill it with the value of another variable that has already been declared in code. You are either on to something profound or have made a bet with someone -- not sure which. The convention, of course, is to "store" and declare the variables in code.

Regards,
Tim
 
Rich said:
Milo, which naming convention should we follow?

Well, I prefer to put a Mile before my object names such as Mile's bed, Mile's television, Mile's beer, and Mile's pet vole!

You might try using a Rich before your objects such as Rich's blue stockings.

:D
 
hi pono,
as Einstein said: "There are only two things infinite - the universe and human stupidity and I am not sure in the former one."

No I didn't make a bet, so only the profound bit remains - could be, here is what I am trying to do.

I have a large number of functionalities in the application. Whether a functionality could be called is dependent on the current state of the application. I store the state of the application in public variables - what has been selected for this and that by other functionalities. They are properly declared variables and by the time the functionlity is called some of them will have a value assigned to them.

All the functionalities are all called from a custom menubar through the same function (with different arguments). Before the functionality is actually called I want the function to check the current state of the application if the current state of the application permits the call of the functionality.

I created a table that lists all my functionalities togather with the form name that plays that functionality.
I created an other table listing the variables needed to have a value to execute the functionality and a reference to the functionality by which you can assign or change the variable value. - This will also be very helpful for me to document the functionalities.

When the user wants to execute a functionality I first want to go through the requirements table to display all the values of the variables the functionality will use to initiate and a link to the functionality the user could use to change that value.

If all required variables have a value and the user is happy with them he can execute the functionality.

The bit that I am stuck with is to get the values of the variables the names of which I retrieve from the table.

The Eval() function would be the obvious solution, but it is stupid, it won't evaluate an expression that contains a variable.

I hope it made clear the purpose, if that helps to find a solution.

Thanks for the effort.
SWK
 
Mile the problem with the prefix is I have so many objects searching for one becomes tedious, I therefore use the suffix
 
hi pono1,
I think the major problem with your method is that you end up storing the values of the variables in the database while they are specific to the application - which makes this approach difficult in a multiuser environment.

Isn't there a collection of which all public variables would be the member of - so I could ask for the value of a member whose name I know?

Thanks for the help.
SWK
 
I think your difficulty is that you are trying to indirectly reference a variable from another variable. This is an example of self-modifying code. But that is (generally) bad program design.

You can't do this in VBA (that I know of) because the time at which the variable name is still available is not when you would like it to be. I.e. when you have the name of the variable you want to select, the symbol table name is no longer available. And VBA is one of those languages that does not contain a true "pointer" type of variable so you can't approach it that way.

Now if you had a record containing all the possible variables as part of a recordset, you could try this:

stSelectorName = "MyVar"

stValue = recordset.Fields(stSelectorName)

Which would work correctly if you had a field name "MyVar" in the recordset.

But the corresponding syntax doesn't necessarily work for a module because of visibility and recursion issues. For instance, suppose the variable you wanted was public and in a general module, but it was in a function or subroutine for which recursion was a very real possibility? Which instantiation of the public variable is the one you want? The Module object does not have a collection called "Variables" in it, so there is no syntax analogous to the .Fields syntax for recordsets and tabledefs.

For that matter, this kind of reference doesn't work in ANY language I know of that goes through a compile or semi-compile (meta-code) phase. And VBA does, indeed, go through a semi-compile. VB goes through a full compile. Only fully interpretive languages that support run-time variable substitution can support this type of feature. This is something like you might do in a command-line language such as DOS .BAT files or OpenVMS .COM files or UNIX interpretive shell scripts.

To do this in code to the RIGHT of an assignment statement, you might try a switch function or a Select Case syntax. This could maybe work if all you are going to do is try to get back a value based on some other item's value as a selector.

Look up Switch in the Help files. Basically, it is a run-time text-oriented Case statement. Not very efficient and the more options you give it, the clunkier it is. But it is the only way I can think of offhand to do what you are trying to do. And I'll not make any guarantees even for Switch. For instance, if you try to do this in a query outside the module containing the variable name, you are still in the same boat you were in earlier. Because in order to use this, you MUST have something in the context of the module containing the named variable.

Don't even BEGIN to ask about how to make this variable the LEFT side of your assignment statement. If you have to do this, you are doing something wrong. Trust me on this. No matter who said you should do such a thing, you have my permission to slap them up the side of the head with a very wet rubber chicken.

(Well, you COULD do the left-hand-side case in code with another Select Case... but again, yecch!)

So, now having pooh-poohed your idea, here is how you do it in code (but still not in queries...) Like I said, you cannot reference a variable this way. So for this to work, it cannot be a variable. Make it something else that CAN be reference by run-time text resolution. Like a field.

Create a table that holds all such variables that can be referenced in this manner. Each field in the table is one of your variables. Empty the table (erase it so it has no records.)

Now open a table-type recordset to this table. Do an .AddNew on it. Load initial values to all variables contained therein. Update the recordset but do not do a .Move variant.

From that point forward, you can use syntax as shown earlier.

recset.Fields(fieldname) = x ...
recset.Update

or

x = recset.Fields(fieldname)...

where fieldname is the name of the variable in which you have the name of the REAL variable, which MUST match one of the fields in the recordset.

Always, always, always remember to close the recordset when you are done. Always always always remember to ERASE the recordset when appropriate.

Remember that a record in a table has approximately a 2Kb limit when doing this. So if you have too many variables for this to be held in a single record, use more than one recordset.

And if you tell anyone I told you how to do this, I'll deny it on a stack of bibles.
 
I think I found a solution but I am very interested in your opinion.

It uses the Scripting Runtime library (I don't know the heart and soul of these beasts - I mean different languages or wet chickens - but but it sounds very similar to what you said DocMan that I need to look at scripting because at compiling I loose the variable names.)

Also pono1 thanks for pointing out the possibility of using a different variable type - collection, array, associative array (dictionary).

Module1
Public AppState As New Dictionary

Form1
AppState.Add "a", "Athens"
AppState.Add "b", "Belgrade"
AppState.Add "c", "Cairo"
AppState.Add "d", "Doncaster"
AppState.Add "e", "Eastwood"

Form2
'Get VarName from a table - it will be a string
VarName = "a"
MsgBox "Item selected: " & AppState.Item(VarName)

Here a, b, c, etc. would be my variable names that I could store in the database.

Thanks for your help.
SWK
 
Last edited:

Users who are viewing this thread

Back
Top Bottom