表值更新将包含where语句

时间:2019-04-06 19:31:54

标签: vba sql-update

我有一个表,该表的字段名称为“ StepX”,其中X是10到1420,步进10s。我有另一个表,该表的行值为10到1420,并且两者之间的这些值已连接。我需要一个例程,该例程从字段名称中获取“ X”,在另一张表中找到等效的“ X”值行,从第二张表中的列(X所在的同一行)中获取值,并将该行字段名称“ StepX”下的第一张表中的值。

浏览了各种问题和答案,但在遍历过程中找不到如何处理字段名称更改的方法。直到工作,但我看不到第一张表中的值。

Dim dbs As Database
   Dim MarkerNumber As Integer
   Dim MarkerTotal As Integer
   Dim stSQL As String


   MarkerTotal = 1420
   MarkerNumber = 10

   Set dbs = CurrentDb

    Do Until MarkerNumber = MarkerTotal

        stSQL = "UPDATE tbl_Changes SET tbl_Changes.[Step'markernumber'] = SELECT_TypeTest set TypeTest WHERE SELECT_TypeTest.Order =10"

        MarkerNumber = MarkerNumber + 10
    Loop

我输入了您所拥有的内容,但是它没有用,可能是因为我没有给您完整的故事。自从我提交问题以来,我按如下方式更改了更新查询,并且可以正常运行:UPDATE tbl_Changes,SELECT_TypeTest SET tbl_Changes.step10 = IIf([SELECT_TypeTest]![InstrTest]> 0,“ Need”,“ N / A”) 在哪里((((SELECT_TypeTest.Order)= 10)); 需要帮助,使用MarkerNumber排序器将其转换为代码。

1 个答案:

答案 0 :(得分:0)

微小变化

   Dim dbs As Database
   Dim MarkerNumber As Integer
   Dim MarkerTotal As Integer
   Dim stSQL As String


   ''MarkerTotal = 1420
   ''MarkerNumber = 10

   Set dbs = CurrentDb

   For MarkerNumber = 10 To 1420 Step 10

        stSQL = "UPDATE tbl_Changes SET Step" & Trim(Str(MarkerNumber)) & " = TypeTest " & _
               " FROM ( SELECT TypeTest FROM  SELECT_TypeTest  WHERE Order = " & Trim(Str(MarkerNumber))

        ' now that the sql is built, then execute it
        dbs.Execute

   Next