5

I have a variable strFunction, then I have another string strName = "strFunction" , what I want to know is how can I get the value of strFunction by using strName.

For example, something like getValue(strName) gives me the value of strFunction. Is it possible in Access VBA?

Thanks!

EDIT:

I have a strFunction string, it's a const string. In my code I want to use Len("strFunction") to test the length of it, but what i got is the length "strFunction". So I need a get-value-out-of-variable-name function. I have tried Eval(), but it cannot do this, even I write a get_strFunction(), eval("get_strFunction()") gives me error, telling me it cannot find it.

Private Const strFunction as String = "FilterByType_1"
Private Function get_strFunction()
    get_strFunction = strFunction
End Function
1
  • Works for me, if I change Private Function to Public Function Commented Jul 11, 2011 at 8:56

2 Answers 2

4

"I have a variable strFunction, then I have another string strName = "strFunction" , what I want to know is how can I get the value of strFunction by using strName."

Instead of a variable, strFunction could be the key for an item in a VBA collection.

Public Sub darkjh()
    Dim strName As String
    Dim col As Collection
    Set col = New Collection
    col.Add "FilterByType_1", "strFunction"
    strName = "strFunction"
    Debug.Print col(strName)
    Set col = Nothing
End Sub

Edit: Instead of a VBA collection, you could use a Scripting.Dictionary.

Dim strName As String
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "strFunction", "FilterByType_1"
strName = "strFunction"
Debug.Print dict(strName)
Set dict = Nothing
Sign up to request clarification or add additional context in comments.

3 Comments

OH just like a dictionary in Python. I'll try it out tomorrow!
Yes. I added a Scripting.Dictionary example, which you might prefer if you're a Python person.
These 2 methods also work in VB6 as a way to replace a variable-string name inside a string by the contents of that variable-string. (Note for VB6 users, as Mods deleted my VB6 specific question).
0
Option Compare Database

Dim a As String
Dim b As String

Public Sub test()
    a = "b"
    b = "test-string"
    Debug.Print Eval("get" & a & "()")
End Sub

Public Function getB() As String
    getB = b
End Function

Output

>>test
test-string

eval(a) did not work, so I had to write a "getter" for the variable and eval that function: eval("get" & a & "()").

3 Comments

eval function cannot get a value of a variable... eval("strFunction") gives me error.
@darkjh update the question with your code, so we can help you.
If you're using Eval(), you want eval(strFunction) instead of eval("strFunction") -- putting the quotes in forces Eval() to look at the literal string of the variable name, whereas leaving out the quotes causes Eval() to look at the string stored in the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.