Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 对较短代码使用数组_Arrays_Vba_Ms Access - Fatal编程技术网

Arrays 对较短代码使用数组

Arrays 对较短代码使用数组,arrays,vba,ms-access,Arrays,Vba,Ms Access,我重复了同一句话很多次,只是做了一些小改动。我喜欢通过使用对象数组来缩短它 例如,代替此代码: StartUpdateStr = "Update tblAfterSale SET " EndUpdateStr = " WHERE IDAfterSale = " IDAfterSale = Me.lblIDAfterSale.Caption db.Execute StartUpdateStr & "Data1 = " & Me.Lable1.Caption & EndUp

我重复了同一句话很多次,只是做了一些小改动。我喜欢通过使用对象数组来缩短它

例如,代替此代码:

StartUpdateStr = "Update tblAfterSale SET "
EndUpdateStr = " WHERE IDAfterSale = "
IDAfterSale = Me.lblIDAfterSale.Caption

db.Execute StartUpdateStr & "Data1 = " & Me.Lable1.Caption & EndUpdateStr & IDAfterSale
db.Execute StartUpdateStr & "Data2 = " & Me.Lable2.Caption & EndUpdateStr & IDAfterSale
db.Execute StartUpdateStr & "Data3 = " & Me.Lable3.Caption & EndUpdateStr & IDAfterSale
db.Close
我在找这样的东西:

Const dCaption = "Me.Lable1.Caption,Me.Lable2.Caption,Me.Lable3.Caption"
Public d(2) As Integer

Public Sub MyMacro()
    Dim vntTemp As Variant
    Dim intIndex As Integer
    vntTemp = Split(lCaption, "d")

    For intIndex = 0 To 2
        db.Execute StartUpdateStr & "Data"& intIndex &  " = " & d(intIndex) & EndUpdateStr & IDAfterSale
    Next
End Sub
有人能给我写正确的语法吗?
谢谢

您只需使用
Me(“标签”&i)


我想你将来会添加很多标签。那么你能使用下面的代码吗

Private Sub PrintAllLabel()
     For Each ctl In Me.Controls
         If TypeName(ctl) = "Label" Then
              db.Execute StartUpdateStr & "Data" & intIndex &  " = " _
              & ctl.Caption _
              & EndUpdateStr & IDAfterSale
         End If
     Next ctl
 End Sub

您可以在一个查询中更新多个列:
update T set a=1、b=2、c=3,其中…
,因此不需要三次访问数据库。您应该使用参数化插入来避免注入漏洞。谢谢大家。所有3条注释都对我有帮助:)我的代码现在好多了。
Private Sub PrintAllLabel()
     For Each ctl In Me.Controls
         If TypeName(ctl) = "Label" Then
              db.Execute StartUpdateStr & "Data" & intIndex &  " = " _
              & ctl.Caption _
              & EndUpdateStr & IDAfterSale
         End If
     Next ctl
 End Sub