Reference a variable with another variable

  • Thread starter Thread starter MikeC
  • Start date Start date
M

MikeC

Can anyone tell me a way to reference a variable using
another variable?

I've written a function that I would like to use to update
a public variable passed to it by the calling procedure.

I've tried using the Eval() function, but I receive the
below error.

"Database can't find the name 'MyVariable' you entered in
the expression."

A snippet of the code is below (strVariable="MyVariable"):

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Private Function fnHyperLinkOnCurrent(ctlTxtBox As
Control, ctlHyperLink As Control, strVariable As String)

If Not IsNull(ctlTxtBox) Then
ctlHyperLink.Caption = ctlTxtBox.Value
ctlHyperLink.HyperlinkAddress = fnPath() & "\" &
ctlTxtBox.Value
Eval(strVariable) = ctlTxtBox.Value
Else
ctlHyperLink.Caption = ""
ctlHyperLink.HyperlinkAddress = ""
Eval(strVariable) = ""
End If
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Any useful ideas would be greatly apprecicated. Thanks.
 
You can make a public function, read your variable there and use this public
function in eval. one function per each variable. if you have a lot of
variables - then suggest to use a collection
 
Why not just pass the public var in the call??

fnHyperLinkOnCurrent me.MyContorl,my.MyHLink,gblPublicVar

Private Function fnHyperLinkOnCurrent(ctlTxtBox As
Control, ctlHyperLink As Control, gblVar)


Then use:

gblVar = = ctlTxtBox.Value
 
That solution passes the value of the variable as opposed
to passing the variable itself. I was attempting to
update the variable within the function.

I have implemented an alternative solution where the
function returns a value and then I set my public variable
equal to the value.

For another similar function, I implemented a solution
very similar to the one you suggested below. I passed the
value of my public variable to the function and then I had
the function return a new replacement value to the calling
procedure.

Thanks to all for the ideas.
 
You can add the "ByRef" argument to a sub or function to have it actually
change the value of variables passed to it.

Private Sub doByRef(ByRef varRef As Variant)

varRef = varRef + 1

End Sub

intMyVar = 1
Call doByRef(intMyVar)
MsgBox intMyVar ' Should be 2

The default is "ByVal" which passes just the value, as you observed.
 
The default is "ByVal" which passes just the value, as you observed.

No it isn't: the default is by reference. ByVal is helpful to protect
formal parameters. ByRef is hardly ever needed but aids readability.

HTH


Tim F
 
Back
Top