每个字段的ms访问权限

时间:2017-03-12 01:17:10

标签: ms-access access-vba

我正在尝试将表中的所有Null(tblIdea)设置为NS,但是下面的代码会抛出一个' Object Required'错误并突出显示以' If。'

开头的行
Sub CommitNS()

Dim db As dao.Database
Dim tdf As dao.TableDef
Dim fld As dao.Field

Set db = CurrentDb
Set tdf = db.TableDefs("tblIdea")

For Each fld In tdf.Fields
  If fld Is Null Then
  fld = "NS"
End If

Next fld

Set tdf = Nothing
Set db = Nothing

End Sub

1 个答案:

答案 0 :(得分:3)

您错过了实际更新:

Sub CommitNS()

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim fld As DAO.Field
    Dim Update As Boolean

    Set db = CurrentDb
    Set rst = db.OpenRecordset("Select * From tblIdea")

    While Not rst.EOF
        For Each fld In rst.Fields
            If IsNull(fld.Value) Then
                Update = True
                Exit For
            End If
        Next
        If Update = True Then
            ' Record needs update.
            rst.Edit
                For Each fld In rst.Fields
                    If IsNull(fld.Value) Then
                        fld.Value = "NS"
                    End If
                Next
            rst.Update
            Update = False
        End If
        rst.MoveNext
    Wend
    rst.Close

    Set fld = Nothing
    Set rst = nothing
    Set db = Nothing

End Sub