Me again. 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 & ” Is Null”)
Here is my code:
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"
End Sub
… 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")That makes sense. If the list box selections were “field1”, “field3”, and “field4”, your
DCount()would throw error #3075, “Syntax error (comma) in query expression ‘field1,field3,field4 Is Null’.“The
DCount()expression would be evaluated like this SELECT statement:And the db engine will definitely complain about that
WHEREclause. You can’t give it a list of field names and ask it whether the listIs Null. You would have to separately ask whether each fieldIs Null.That is the explanation for why you get that error. However, I think you need to change your approach.
Build up the
RowSourcevalue list string forFields_Valuesas you iterate throughList101selected items. (Make sure you have Value List as the Row Source Type property forFields_Values.) Here is a tested sample which does what I think you want.Also I don’t see why the user should even be allowed to click
Command29unlessList101contains selected items. So consider disablingCommand29until selections have been made.Finally, I’ll suggest you do yourself a favor by giving your controls meaningful names …
Command29could becmdShowNullCountsandList101could belstFieldNames. Meaningful names are especially helpful when you’re dealing with dozens of controls. You should also find them helpful when you need to revisit the form design after a few months. Anyone else who has to take over in the future will appreciate your courtesy.