对于单个字段,您可以使用简单查询。
SELECT Count(*) AS CountOfNulls
FROM MyTable
WHERE some_field Is Null;
如果要在单个查询中分别计算多个字段的 Null,可以执行以下操作:
SELECT
Sum(IIf(some_field Is Null, 1, 0)) AS NullsIn_some_field,
Sum(IIf(another_field Is Null, 1, 0)) AS NullsIn_another_field
FROM MyTable;
如果您想要所有 Null 的总计,而不是每列的计数,您可以将前一个查询用作子查询并将各个列计数相加。
SELECT base.NullsIn_some_field + base.NullsIn_another_field AS total_nulls
FROM
(
SELECT
Sum(IIf(some_field Is Null, 1, 0)) AS NullsIn_some_field,
Sum(IIf(another_field Is Null, 1, 0)) AS NullsIn_another_field
FROM MyTable
) AS base;
OTOH,如果您希望完全避免使用 SQL,或者只是觉得这些语句太复杂,则不需要使用 SQL。您可以DCount()
在 VBA 过程中使用该函数。
使用数据库中表的名称在“立即”窗口中运行以下过程:
HowManyNulls "YourTable"
CTRL您可以使用+g键盘快捷键转到“立即”窗口。
Public Sub HowManyNulls(ByVal pTable As String)
Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdf As DAO.TableDef
Dim lngNulls As Long
Dim lngTotal As Long
Set db = CurrentDb
Set tdf = db.TableDefs(pTable)
For Each fld In tdf.Fields
'lngNulls = DCount("*", pTable, fld.Name & " Is Null")
' accommodate field names which need bracketing ...
lngNulls = DCount("*", pTable, "[" & fld.Name & "] Is Null")
lngTotal = lngTotal + lngNulls
Debug.Print fld.Name, lngNulls
Next fld
Debug.Print "Grand total", lngTotal
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
如果这些建议都不令人满意,请修改您的问题以帮助我们更好地了解您的需求。