带有 SQL 语句的 DAO 记录集使用 For Next 循环添加记录

DAO Recordset with SQL Statement to Add Records with For Next Loop

我知道这很愚蠢,但我想不通。下面的当前代码在 TxtBox1 中插入满足条件的记录集,然后通过为满足 TxtBox2 条件的新记录集重写来擦除 Table 中的数据。

我需要的是将 TxtBox1 中满足条件的记录集插入 Table,然后在循环时将记录集的下一次迭代添加到 Table。 (不重写/删除第一个记录集return)

Dim db As Database
Dim rst As DAO.Recordset
Dim rrow As Integer, strCmboNmbr As Integer
Dim start As Object

Set db = CurrentDb()

strCmboNmbr = (Left(Forms("Match Summary").Controls("CmbNum").Value, 1))

For rrow = 1 To strCmboNmbr 'lets say 2
  Set start = Forms("Match Summary").Controls("TxtBox" & rrow) 'TxtBox1, TxtBox2, etc.
    DoCmd.RunSQL "SELECT [Oracle JE].* INTO Unmatched FROM [Oracle JE] WHERE [Oracle JE].[Account Code]=" & start 'This is a MAKE Table Query
Next rrow

如果您不想覆盖 table,您需要在第一个 运行-through:

之后追加
If rrow = 1 then
    DoCmd.RunSQL "SELECT [Oracle JE].* INTO Unmatched FROM [Oracle JE] 
    WHERE [Oracle JE].[Account Code]=" & start 
Else
    DoCmd.RunSQL "INSERT INTO Unmatched SELECT [Oracle JE].* FROM [Oracle JE] 
    WHERE [Oracle JE].[Account Code]=" & start 
End

理想情况下,您应该有一个预定义的 table。然后你可以给它添加一个主键和索引,先清除它然后运行一系列追加。