1

又是我。This code works fine when only 1 item is selected, however when more than one item is selected I get the error "syntax error (comma) in query expression" on the following line of code: intCountNull = DCount("*", "Scrubbed ", strCriteria & " 为空")

这是我的代码:

Private Sub Command29_Click()
Dim strCriteria As String
Dim intCountNull As Integer
Dim varItem As Variant

'On Error GoTo Err_Command29_Click

Me.Fields_Values.RowSource = ""

For Each varItem In Me!List101.ItemsSelected
strCriteria = strCriteria & "," & Me!List101.ItemData(varItem)
Next varItem

strCriteria = Right(strCriteria, Len(strCriteria) - 1)

intCountNull = DCount("*", "Scrubbed", strCriteria & " Is Null")
Fields_Values.RowSource = intCountNull & " null values found in " & strCriteria

Exit_Command29_Click:
    Exit Sub
'Err_Command29_Click:
    'MsgBox "Please select a field"

结束子

4

1 回答 1

0

... when more than one item is selected I get the error "syntax error (comma) in query expression" on the following line of code:intCountNull = DCount("*", "Scrubbed", strCriteria & " Is Null")

那讲得通。如果列表框选择为“field1”、“field3”和“field4”,您DCount()将抛出错误 #3075,“查询表达式 'field1,field3,field4 Is Null'中的语法错误(逗号)。

DCount()表达式将像以下 SELECT 语句一样计算:

SELECT Count(*)
FROM Scrubbed
WHERE field1,field3,field4 Is Null

数据库引擎肯定会抱怨该WHERE条款。您不能给它一个字段名称列表并询问它是否列表Is Null。您将不得不单独询问每个字段是否Is Null.

这就是为什么您会收到该错误的解释。但是,我认为你需要改变你的方法。

在您遍历选定项目时构建RowSource值列表字符串。(确保您将值列表作为 . 的行源类型属性。)这是一个经过测试的示例,它可以满足您的需求。 Fields_ValuesList101Fields_Values

Private Sub Command29_Click()
    Dim strCriteria As String
    Dim intCountNull As Integer
    Dim varItem As Variant
    Dim strRowSource As String

    Me.Fields_Values.RowSource = ""
    For Each varItem In Me!List101.ItemsSelected
        strCriteria = "[" & Me!List101.ItemData(varItem) & _
            "] Is Null"
        intCountNull = DCount("*", "Scrubbed", strCriteria)
        'Debug.Print strCriteria, intCountNull '
        strRowSource = strRowSource & ";" & intCountNull & _
            " null values found in " & _
            Me!List101.ItemData(varItem)
    Next varItem

    'Debug.Print strRowSource '
    If Len(strRowSource) > 0 Then
        strRowSource = Mid(strRowSource, 2)
        Me.Fields_Values.RowSource = strRowSource
    End If
    'Debug.Print strRowSource '
End Sub

此外,我不明白为什么用户甚至应该被允许点击Command29,除非List101包含选定的项目。所以考虑禁用Command29,直到做出选择。

Private Sub Form_Load()
    Me.Command29.Enabled = False
End Sub

Private Sub List101_AfterUpdate()
    Me.Command29.Enabled = (Me.List101.ItemsSelected.Count > 0)
End Sub

最后,我建议你帮自己一个忙,给你的控件起有意义的名字...... Command29可能是cmdShowNullCounts并且List101可能是lstFieldNames。当您处理数十个控件时,有意义的名称特别有用。当您需要在几个月后重新访问表单设计时,您也会发现它们很有帮助。将来必须接任的任何其他人都会感谢您的礼貌。

于 2012-08-07T20:03:34.387 回答