Office中国论坛/Access中国论坛

标题: [求助]一个for循环的问题(无法用n构成变量名) [打印本页]

作者: tz-chf    时间: 2006-12-27 04:30
标题: [求助]一个for循环的问题(无法用n构成变量名)
dim n as integer

dim str as string

dim strA_1 as string

dim strA_2 as string

dim strA_3 as string

'还有几十个,以3个为例

strA_1="11"

strA_2="22"

strA_3="33"

for n=1 to 3

    str=str & strA_n   '在这里用n生成变量名,无法解决

next n

无法生成变量名,窗体里有没有自定义变量的集合?有集合就好办了。不行的话只能用数组了。
作者: wuaza    时间: 2006-12-27 05:09
声明数组

dim strA() as string

若数组的大小被指定的话,则它是个固定大小数组。若程序运行时数组的大小可以被改变,则它是个动态数组。

[此贴子已经被作者于2006-12-26 21:10:22编辑过]


作者: andymark    时间: 2006-12-27 05:11
转换这样,看看行不

Dim Str As String

Dim StrA As String

For n = 1 To 3

Select Case "strA_" & n
      
    Case "StrA_1"
    StrA = "11"
    Case "StrA_2"
    StrA = "22"
    Case "StrA_3"
    StrA = "33"
    End Select
    Str = Str & StrA

Debug.Print Str
Next

[此贴子已经被作者于2006-12-26 21:13:26编辑过]


作者: tz-chf    时间: 2006-12-27 05:21
谢了!

正是因为有数十个变量,代码太长,所以用循环。看来只能用数组了。




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3