选择带空白字段的记录

时间:2014-05-26 10:21:07

标签: ms-access access-vba

我有一个包含多个标题的访问数据库

对于这个问题,我只想说我有header1,header2和header3

我想要做的是获取header3的空白字段的最后一项,因为header1和header2都已包含数据。

我现在拥有的东西:

 Dim db As DAO.Database
 Dim rst As DAO.Recordset
 Dim sqlstr As String
 Dim header1, header2, header3 as field

 Set db = CurrentDb()
 Set rst = db.OpenRecordset("TblToMonitor")

 sqlstr = "Select * from [Mytable] Where [header3] IS NULL OR len(Nz([header3])) =0;"

 docmd.runsql(sqlstr)

 with rst
      .edit
      .fields("header3") = "Done"
      .update
 end with

当我运行代码时,它选择第一行,然后用done更新header3字段,但不移动到下一条下一条记录。

任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:2)

为了更新所有行,您需要将更新部分包含在循环中:

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sqlstr As String
Dim header1, header2, header3 as field

Set db = CurrentDb()    

sqlstr = "Select * from [Mytable] Where [header3] IS NULL OR len(Nz([header3])) =0;"

Set rst = db.OpenRecordset(sqlstr) 
with rst
    While not .EOF
        .edit
        .fields("header3") = "Done"
        .update
        .MoveNext
    Wend
end with