遍历表,更改字段值遍历、字段

2023-09-08 11:23:17 作者:嗜酒如命

我想写一张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