我可以在更新过程中检查 DAO Recordset 字段中的值吗?
Can I check Value in DAO Recordset field during update process?
我想通过查看对 RecordSet 的一些影响来结束 table 中更新数据的循环。
在这种情况下:假设我有 Table1
字段:GroupID
,一个 Long,和 Val
,一个 Double。无主键 不限制引用一个 GroupID
的值,不限制单个 GroupID
.
的记录数
Dim ID as Long
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("SELECT GroupID, SUM(Val) As ValSum FROM Table1 Order By SUM (Val) ASC Group By GroupID")
rst.MoveFirst
ID = rst.Fields(0)
Do while ID = rst.Fields(0)
CurrentDB.Execute("INSERT INTO Table1 (GroupID, Val) VALUES (" & ID & ", 1)")
rst.Requery()' Change value of first rst?...
Loop
'next line of code...
rst
是先指向SUM(Val)
最低的GroupID,我再添加记录应该加到值SUM(Val)
,最后还有一些其他的GroupID
在 rst
的第一条记录中。
这应该会导致 while ID = rst.Fields(0)
的值为 False
,然后退出循环。
在实践中,我得到了一个无限循环。
有没有办法更新 DAO.Recordset 而 运行 的值?
您是想赢得最令人困惑的代码竞赛还是...?
无论如何,您的代码在更正一些错误后对我有用(您的 SQL 语法错误,Val 是保留字)。
Sub TestConfusingRS()
Dim ID As Long
Dim S As String
Dim rst As DAO.Recordset
Dim i As Long
S = "SELECT GroupID, SUM(myVal) As ValSum FROM Tabelle1 Group By GroupID Order By SUM(myVal) ASC"
Set rst = CurrentDb.OpenRecordset(S)
ID = rst.Fields(0)
Do While ID = rst.Fields(0)
i = i + 1
Debug.Print i, "Inserting GroupID " & ID
CurrentDb.Execute "INSERT INTO Tabelle1 (GroupID, myVal) VALUES (" & ID & ", 1)"
rst.Requery
' Prevent endless loop
If i > 100 Then Exit Do
Loop
End Sub
与
+---------+-------+
| GroupID | myVal |
+---------+-------+
| 1 | 6 |
| 2 | 2 |
| 2 | 1 |
| 3 | 5 |
+---------+-------+
我明白了
1 Inserting GroupID 2
2 Inserting GroupID 2
到此为止:
+---------+-------+
| GroupID | myVal |
+---------+-------+
| 1 | 6 |
| 2 | 2 |
| 2 | 1 |
| 2 | 1 |
| 2 | 1 |
| 3 | 5 |
+---------+-------+
我想通过查看对 RecordSet 的一些影响来结束 table 中更新数据的循环。
在这种情况下:假设我有 Table1
字段:GroupID
,一个 Long,和 Val
,一个 Double。无主键 不限制引用一个 GroupID
的值,不限制单个 GroupID
.
Dim ID as Long
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("SELECT GroupID, SUM(Val) As ValSum FROM Table1 Order By SUM (Val) ASC Group By GroupID")
rst.MoveFirst
ID = rst.Fields(0)
Do while ID = rst.Fields(0)
CurrentDB.Execute("INSERT INTO Table1 (GroupID, Val) VALUES (" & ID & ", 1)")
rst.Requery()' Change value of first rst?...
Loop
'next line of code...
rst
是先指向SUM(Val)
最低的GroupID,我再添加记录应该加到值SUM(Val)
,最后还有一些其他的GroupID
在 rst
的第一条记录中。
这应该会导致 while ID = rst.Fields(0)
的值为 False
,然后退出循环。
在实践中,我得到了一个无限循环。
有没有办法更新 DAO.Recordset 而 运行 的值?
您是想赢得最令人困惑的代码竞赛还是...?
无论如何,您的代码在更正一些错误后对我有用(您的 SQL 语法错误,Val 是保留字)。
Sub TestConfusingRS()
Dim ID As Long
Dim S As String
Dim rst As DAO.Recordset
Dim i As Long
S = "SELECT GroupID, SUM(myVal) As ValSum FROM Tabelle1 Group By GroupID Order By SUM(myVal) ASC"
Set rst = CurrentDb.OpenRecordset(S)
ID = rst.Fields(0)
Do While ID = rst.Fields(0)
i = i + 1
Debug.Print i, "Inserting GroupID " & ID
CurrentDb.Execute "INSERT INTO Tabelle1 (GroupID, myVal) VALUES (" & ID & ", 1)"
rst.Requery
' Prevent endless loop
If i > 100 Then Exit Do
Loop
End Sub
与
+---------+-------+
| GroupID | myVal |
+---------+-------+
| 1 | 6 |
| 2 | 2 |
| 2 | 1 |
| 3 | 5 |
+---------+-------+
我明白了
1 Inserting GroupID 2
2 Inserting GroupID 2
到此为止:
+---------+-------+
| GroupID | myVal |
+---------+-------+
| 1 | 6 |
| 2 | 2 |
| 2 | 1 |
| 2 | 1 |
| 2 | 1 |
| 3 | 5 |
+---------+-------+