遍历表,更改字段值 [英] Looping Through Table, Changing Field Values

查看:154
本文介绍了遍历表,更改字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一张code将通过一个表上运行,并更换有一定的价值与其他价值各个领域。

I'm trying to write a piece of code that will run through a table and replace every field that has a certain value with another value.

Private Sub Form_Load()


Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSQL = "SELECT Profile3 FROM Bank WHERE 'AB'"

Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)

Do Until rst.EOF

With rst

If .RecordCount > 0 Then

  .MoveFirst
  .Edit
  !Profile3 = "AA"
  .Update     
  .MoveNext


End If

End With

Loop

End Sub

这就是我目前使用但是,当它运行崩溃可怕的。我知道基础code工作,因为当我拔出它的工作循环,但仅在第一项。

That's what I'm currently using, however, when it runs it crashes horribly. I know the base code works because when I pull out the loop it works, but only on the first entry.

最喜欢的我似乎有VBA的问题,它可能是,我俯瞰太简单了修复。

Like most of the issues I seem to have with VBA, it's probably an absurdly simple fix that I am overlooking.

感谢您的帮助。

推荐答案

正在不断移动第一。你需要得到在某个阶段。刚刚摆脱的MoveFirst的。

You are constantly moving first. You need to get on at some stage. Just get rid of MoveFirst.

Do Until rst.EOF
    With rst
      .Edit
      !Profile3 = "AA"
      .Update     
      .MoveNext
    End With
Loop

另外,我猜你的意思是WHERE somefield:

In addition, I guess you mean WHERE somefield:

strSQL = "SELECT Profile3 FROM Bank WHERE somefield='AB'"

然而,在这种情况下,我怀疑你需要的是:

However, in this case, I suspect what you need is:

strSQL = "UPDATE Bank SET Profile3 ='AA' WHERE Profile3 ='AB'"
CurrentDB.Execute strSQL, dbFailOnError

这篇关于遍历表,更改字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆