如何在 VBA 中使用字符串获取对变量的引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6646864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:37:06  来源:igfitidea点击:

How can I get reference to a variable by using a string, in VBA?

ms-accessvba

提问by darkjh

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.

我有一个变量strFunction,然后我有另一个字符串strName = "strFunction",我想知道如何使用 strName 获取 strFunction 的值。

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

例如,像 getValue(strName) 这样的东西给了我 strFunction 的值。是否可以在 Access VBA 中使用?

Thanks!

谢谢!

EDIT:

编辑:

I have a strFunctionstring, 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.

我有一个strFunction字符串,它是一个常量字符串。在我的代码中,我想用来Len("strFunction")测试它的长度,但我得到的是长度“strFunction”。所以我需要一个 get-value-out-of-variable-name 函数。我试过 Eval(),但它不能这样做,即使我写了一个get_strFunction(),也会eval("get_strFunction()")给我错误,告诉我它找不到它。

Private Const strFunction as String = "FilterByType_1"
Private Function get_strFunction()
    get_strFunction = strFunction
End Function

采纳答案by HansUp

"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."

"我有一个变量 strFunction,然后我有另一个字符串 strName = "strFunction" ,我想知道如何使用 strName 获取 strFunction 的值。"

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

strFunction可以不是变量,而是VBA 集合中项目的键。

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.

编辑:您可以使用 Scripting.Dictionary 代替 VBA 集合。

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

回答by Jacob

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 & "()").

eval(a)没有用,所以我不得不为变量编写一个“getter”并评估该函数:eval("get" & a & "()").