1

我试过使用 DCOUNT 和 SQL,但没有任何效果。我在下面粘贴了两个查询。当我运行 SQL 时,列表框中没有任何内容。当我运行 DLOOKUP 时,我收到错误消息“运行时错误 '2001”:您取消了之前的操作。组合框名称为 ScrubbedList。表名为 Scrubbed。

DCOUNT

Dim strScrubbedValue As String
strScrubbedValue = Me.ScrubbedList
Dim intCountNull As Integer

intCountNull = DCount("*", "Scrubbed", "IsNull" & strScrubbedValue)
Text267 = intCountNull

SQL

Dim strSQL As String
Dim strScrubbedValue As String

strScrubbedValue = Me.ScrubbedList
strSQL = "SELECT Count(*) As CountAll" & strScrubbedValue & " FROM Scrubbed"
strSQL = strSQL + "WHERE" & strScrubbedValue = ""
Me.List265.RowSource = strSQL
4

1 回答 1

0

尝试:

 intCountNull = DCount("*", "Scrubbed", "SomeField Is Null")

所以对于组合:

 intCountNull = DCount("*", "Scrubbed", strScrubbedValue & " Is Null")

连接字符串时确保包含相关空格很重要。

Dim strSQL As String
Dim strScrubbedValue As String

strScrubbedValue = Me.ScrubbedList
strSQL = "SELECT Count(*) As CountAll " & strScrubbedValue & " FROM Scrubbed"
strSQL = strSQL & " WHERE " & strScrubbedValue & " Is Null"
Me.List265.RowSource = strSQL

请注意,Null 和零长度字符串 (ZLS) 不同。

要同时获得两者,您可以说:

    strSQL = strSQL & " WHERE " & strScrubbedValue & " & '' = ''"

VBA 中的字符串连接符是 &,而不是 +。加号必须小心使用,因为它可能导致意外的空值。

于 2012-07-16T19:57:53.367 回答