Refering to a variable with a string - please help

greenfalcon

Registered User.
Local time
Today, 04:54
Joined
Aug 22, 2006
Messages
34
I will attempt and keep this short and sweet,

I essentially have a lot of code that is duplicated within a case statement that updates the values of separate variables before performing computations... my question is this, i would like to be able to pass the function the name of a variable and have it update that variable... it would look something like this


Sub test()
tmpResult = updateValues("variable1")
tmpResult = updateValues("variable2")
end sub

Function updateValues(Dim myValue as ??)
myValue = 1 + 2 + unknownFactor
End function


I have made this example really basic, I hope it makes sense, let me know if there are any suggestions.. I know how to refer to an array by name but not strings/integers and such... I just want to pass the name to another variable..

Thanks again

Jason
 
You can go like this:

Code:
Sub test()
    tmpResult = updateValues(variable1)
    tmpResult = updateValues(variable2)
end sub

Function updateValues(myValue1 As Double, myValue2 As Double) As Double
    updateValues = myValue1 + myValue2 + unknownFactor
End function

Something like that.
 
Maybe I misunderstood. You want to update the variable you started with?
 
That is correct, I want to update the variable that i started with...
 
Maybe i can re-write it so it makes a little more sense

Sub test()
tmpResult = updateValues("Name_of_Variable1")
tmpResult = updateValues("Name_of_Variable2")
end sub

Function updateValues(Dim myValue as ??)
myValue = 1 + 2 + unknownFactor
End function

So in turn the sudo code would something be like this if i did it another way

Function ()
Name_of_Variable1 = 1 + 2 + unknownFactor
Name_of_Variable2 = 1 + 2 + unknownFactor
}

I want the function to actually update the value of the variable that i passed to the function..
 
Then something like this, perhaps:

Code:
Sub test()
    variable1 = updateValues(variable1)
    variable2 = updateValues(variable2)
end sub

Function updateValues(ByVal myValue As Double) As Double
    updateValues = 1 + 2 + unknownFactor
End function
 
Ok.. thats not it either, thanks so much for your help... maybe i can better explain by showing you a working example with an array

The code below, if you test it, allows me to refer to an array with a variable... maiking it easy to pass more arrays to the function. I would like to do the same thing with a string where i am able to dynamically refer to that string / integer by its name and manipulate its value.

Thanks again!

* Code below is missing the array declaration and values in array... i hope basic principle is clear enough :)

Code:
Sub build_results()
Dim myArray As Variant ' this allows me to refer to array by string

tmpResult = start_Build(binfFLow)  ' <--- name of my array that i want to refer to

Public Function start_Build(ByRef myArray As Variant)
    minVal = Min(myArray)
End Function

Function Min(ParamArray avValues() As Variant) As Variant
    Dim vThisItem As Variant, vThisElement As Variant
    
    On Error Resume Next
    For Each vThisItem In avValues
        If IsArray(vThisItem) Then
            For Each vThisElement In vThisItem
                Min = Min(vThisElement, Min)
            Next
        Else
            If vThisItem < Min Then
                If Not IsEmpty(vThisItem) Then
                    Min = vThisItem
                End If
            ElseIf IsEmpty(Min) Then
                Min = vThisItem
            End If
        End If
    Next
    On Error GoTo 0
End Function
 
Sorry, arrays are one thing I still haven't gotten my head around. I have not had to use them so I've never really worked with them, except for a single dimension array for the split function. Someone else may have to take this.
 
wow, thanks bob.. you have always been an amazing help.

Thanks again for the support.
Ill keep trying to find out if it is possible.
Jason
 
I'm kind of lost following this thread.

For one things, I'm not comprehending what is the problem we're trying to solve and you seem to have code already... can you please re-state what is the problem with the recent code snippet you posted?

Also, maybe it'd help if you posted some bogus data that would be input via this routine, and what you expect it to output. We can then provide useful suggestions.
 
Jason,

I too am a little lost with respect to what you're trying to accomplish.

"I want the function to actually update the value of the variable that i passed to the function.."

Code:
Private Sub cmdTest_Click()
Dim strTest As String
Dim intTest As Integer

   strTest = "abcdefg"
   intTest = 7

   FixData strTest, intTest
End Sub

' This SubRoutine doesn't return anything, but it receives the ADDRESS of the variables
' holding the real data.
' 
' Any changes made by the Replace function and the following If statement
' will be applied to the real variables in the calling code (strTest & intTest)
''
'
Private Sub FixData(ByRef strInput As String, ByRef intInput As Integer)
  strInput = Replace(strInput, "c", "?")
  If intInput = 7 Then intInput = 9
End Sub

If you omit the ByRef, the subroutine will be working on "a copy of the input arguments" and
will not be able to alter the calling variables (strTest & intTest). You would have to
change it into a function that RETURNED a value.

Now, If you get into ADO, DAO and Access controls, you can pass NAMES of fields/objects around
and affect them with commands like:


VariableName = "Field1"

Me.Controls(VariableName) = "SomeValue"
rst.Fields(VariableName) = "SomeValue"

hth,
Wayne
 
I'm kind of lost following this thread.
I 3rd this motion, but hey Jason, I'm pretty good with arrays myself. if you could word the question a little "different", I bet I could answer it. I don't visit very much, but I do reply to my thread subscriptions.

from what I can tell, you may need to use public variables to change their values. i'm sure you know that publics can be changed from anywhere in the application.

i can't tell where the array idea fits into all of this, but i'm pretty sure you can declare an array that is public, then change the elements in any function that you run. that would be just a little complex, but certainly not impossible....
 

Users who are viewing this thread

Back
Top Bottom