如何在Access中检查空值?

时间:2008-10-26 20:09:26

标签: ms-access null

我是Access的新手。我有一张满是记录的桌子。我想编写一个函数来检查是否有任何id为null或为空。如果是这样,我想用xxxxx更新它。 必须在数据库中的所有表中运行对id的检查。 任何人都可以提供一些示例代码吗?

3 个答案:

答案 0 :(得分:1)

我不确定您是否能够使用Access SQL查找数据库中的所有表。相反,您可能希望编写一些VBA来遍历表并为每个表生成一些SQL。有点像:

update TABLE set FIELD = 'xxxxxx' where ID is null

答案 1 :(得分:0)

查看Nz()函数。它会保持字段不变,除非它们为null,当它用你指定的任何内容替换它们时。

对于合理数量和大小的表格,只需

就可以更快
  • 打开它们
  • 依次按每个字段排序
  • 检查空值并手动替换

最好找出空值的来源并停止它们 - 给出字段默认值,在输入上使用Nz()。并让你的代码处理掉过网络的任何空值。

答案 2 :(得分:-1)

我称之为 UpdateFieldWhereNull 函数,并显示一个调用它的子例程(改编自http://www.aislebyaisle.com/access/vba_backend_code.htm

它会更新 DbPath 参数中的所有表格(未经过测试,请小心处理):

Function UpdateFieldWhereNull(DbPath As String, fieldName as String, newFieldValue as String) As Boolean
    'This links to all the tables that reside in DbPath,
    '  whether or not they already reside in this database.
    'This works when linking to an Access .mdb file, not to ODBC.
    'This keeps the same table name on the front end as on the back end.
    Dim rs As Recordset

        On Error Resume Next

    'get tables in back end database
        Set rs = CurrentDb.OpenRecordset("SELECT Name " & _
                                        "FROM MSysObjects IN '" & DbPath & "' " & _
                                        "WHERE Type=1 AND Flags=0")
        If Err <> 0 Then Exit Function

    'update field in tables
        While Not rs.EOF
            If DbPath <> Nz(DLookup("Database", "MSysObjects", "Name='" & rs!Name & "' And Type=6")) Then

                'UPDATE the field with new value if null
                DoCmd.RunSQL "UPDATE " & acTable & " SET [" & fieldName & "] = '" & newFieldValue & "' WHERE [" & fieldName & "] IS NULL"

            End If
            rs.MoveNext
        Wend
        rs.Close

        UpdateFieldWhereNull = True
End Function


Sub CallUpdateFieldWhereNull()
    Dim Result As Boolean

    'Sample call:
    Result = UpdateFieldWhereNull("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", "ID", "xxxxxx")
    Debug.Print Result
End Sub