【问题标题】:Access VBA for loop refer to variable using iAccess VBA for loop 使用 i 引用变量
【发布时间】:2018-05-07 09:13:43
【问题描述】:

我在 Access 2016 中有一个宏,使用 for 循环检查复选框的值。我想将值提交到表中。 我不想要 true (-1) 的标准复选框值我想要已检查的“滚动”计数的值。

例如 CB1 CB2 CB3 CB4

如果只检查 CB1 和 CB4,那么我想将 CB1 的值 1 和 CB4 的值 2 提交到表中。

我的 for 循环是:

For i = 1 To 8
If Me.Controls("CP" & i) = -1 Then
"CP" & i = I    'ISSUE IS HERE
Debug.Print Me.Controls("CP" & i)
End If
Next i

如何引用变量值?所以 CP & I = CP1、CP2、CP3、CP4 等等……

【问题讨论】:

  • Me.Controls("CP" & i)= I?

标签: vba ms-access for-loop


【解决方案1】:

变量名是静态的,你不能使用语法 "CP" & i 来访问变量。您必须改用数组。

Dim arrCP(8)
For i=Me.controls("CP" & i)=-1 then
arrCP(i)=i
debug.print arrCP(i)
end if
next i

【讨论】:

    【解决方案2】:

    可能是这样,使用countTrue 来确定您要显示的数字。

    Dim countTrue As Long: countTrue = 0
    For i = 1 To 8
        If Me.Controls("CP" & i) = -1 Then
            countTrue = countTrue + 1
            Me.Controls("CP" & i) = countTrue
            Debug.Print Me.Controls("CP" & i)
        End If
    Next i
    

    【讨论】:

      猜你喜欢
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多