VBA通过迭代ADODB结果集删除记录
我正在尝试循环遍历 ADODB 结果集并在条件为真时删除记录。但是,当我执行此操作时,仅删除记录的第一个字段,其余记录仍保留。
有什么想法吗? 我有以下代码:
Set ytdRS = New ADODB.Recordset
ytdRS.Source = SQL_YTD
ytdRS.CursorType = adOpenStatic
ytdRS.LockType = adLockBatchOptimistic
rst.MoveFirst
Do Until rst.EOF
if (value = 1) then
rst.Delete
rst.MoveNext
end if
Loop
I am trying to loop through an ADODB resultset and delete the record if a condition is true. However, when I do this only the first field of the record is deleted the rest of the record remains.
Any Ideas?
I have the following code:
Set ytdRS = New ADODB.Recordset
ytdRS.Source = SQL_YTD
ytdRS.CursorType = adOpenStatic
ytdRS.LockType = adLockBatchOptimistic
rst.MoveFirst
Do Until rst.EOF
if (value = 1) then
rst.Delete
rst.MoveNext
end if
Loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到的一件事是 ytdRS.Open 命令。这可能是问题的(一部分)吗?
编辑:其他一些事情:
ytdRS!FieldName.Value
)还是变量名称。在这种情况下,它是一个变量名。if
语句中使用 MoveNext 会遇到无限循环,因为除非Value ,否则您的 Recordset 不会移动到下一条记录
等于 1。下面的代码对我有用;您必须针对您的特定应用程序对其进行编辑。特别是,您需要编辑
ytdRS.Source
、Open()
方法中的ActiveConnection:=
以及ytdRs! [Order ID].Value = 36
行,与您发布的代码块中的“Value”语句相对应。希望这有帮助!
如果您有任何疑问,请告诉我。
One thing I don't see is the ytdRS.Open command. Could this be (part of) the issue?
EDIT: A few other things:
ytdRS!FieldName.Value
) or a variable name. In this context it is a variable name.if
statement, because your Recordset won't move to the next record unless theValue
is equal to 1.The code below worked for me; you will have to edit it for your specific application. In particular you will need to edit
ytdRS.Source
, theActiveConnection:=
in theOpen()
method, and theytdRs![Order ID].Value = 36
line, to correspond to your "Value" statement in the block of code you posted.Hope this helps!
Please let me know if you have any questions.