Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indirect referencing of a variable

Status
Not open for further replies.

ViolinTim

Technical User
Aug 5, 2002
3
GB
I have a large Select Case statement that assigns a value to a variable (strReplacment).

I have already calculated the variables before the Case statement, does anyone know how to indirectly set a variable?

What I am looking for is something like this

strReplacment = Me.Variable("strVariableName") i.e. the same sort of syntax as Me.Controls("Name").

One further complication is that the code is in a module making reference to Me invalid.
 
You can pass the Form as a parameter into the module. From within the form call a module function/sub

ModuleRoutine Me, OtherVariable

You can then inside the module function do the following:

Public Sub ModuleRoutine (rFrm_TheForm As Form, rStr_VarName As String)

MsgBox rFrm_TheForm.fStr_MsgBoxTitle

provided that fStr_MsgBoxTitle has been declared as public within the declarations sections of the form, or you can execute any of the public routines from the form.

rFrm_TheForm.FunctionName

With respect to the Indirect Addressing, after you include a reference to a ScriptControl you can create that object, indicate which scripting language you want to use "VBScript" and then use the Eval method to deal with the indirection. There are plenty of Eval examples in this or other VB fora.



Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I think I'm missing something, trying to picture all of this. Why would you need to indirectly reference this variable? In your example you want to use strReplacment = Me.Variable("strVariableName"), which means that you would assign the variable name to strVariableName at some point...why not just assign the value of that variable to strReplacment and that point? Like I said, I'm sure there's something I missed while reading this...hope I can help.

Kevin
 
If I understand correctly, strReplacement contains the name of the variable which in turn contains the value upon which to perform the case select. Using the Eval function allows you to get the value of the variable whose name is stored in strReplacement.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
As an example of how to achieve this type of indirection, first include a reference to the Microsoft Script Control, and then on a form, in the declarations section, declare two public variables:

Dim ValueOne as Integer
Dim ValueTwo as Integer

Then drop a button on the form and use the following code
Code:
Private Sub cmdButton_Click()
   
   Dim lObj_ScriptControl     As ScriptControl
   Dim VariableName           As String
   
   Set lObj_ScriptControl = New ScriptControl
   lObj_ScriptControl.Language = "VBScript"
   lObj_ScriptControl.AddObject "Example", Me, True
   
   ValueOne = 4
   ValueTwo = 5
   
   VariableName = "ValueOne"
   MsgBox lObj_ScriptControl.Eval(VariableName)
   VariableName = "ValueTwo"
   MsgBox lObj_ScriptControl.Eval(VariableName)
   VariableName = "SQR(ValueOne + ValueTwo)"
   MsgBox lObj_ScriptControl.Eval(VariableName)
   
   Set lObj_ScriptControl = Nothing
   
End Sub

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top