我正在研究一个带有设计形式的简单读写数据库。
我使用此查询来计算所有行
SELECT COUNT (*) FROM tblData
现在,当我添加过滤器时,我无法计算行数。我尝试了各种查询,这里有一些说明,但我无法解码它并使其成为vba。我需要找到 338559
的所有 待处理 状态Dim db As Database
Dim rs As DAO.Recordset
Dim query As String
Dim i As Integer
query = "SELECT COUNT Status = 'Pending' FROM tblData WHERE UID = 338559"
Set db = CurrentDb
Set rs = db.OpenRecordset(query)
For i = 0 To recordCountOfTheItem
'code to display it to listbox
Next i
感谢您帮助编码员:)
答案 0 :(得分:4)
您可以使用该请求:
SELECT COUNT(*)
FROM tblData
WHERE UID = 338559
AND Status = 'Pending';
答案 1 :(得分:0)
如果条件满足设置为1且不满足设置为NULL,则可以通过CASE
语句实现此目的。这适用于 MySQL
当Count计数时,它不会考虑空值,因此您将获得过滤器的有效计数
SELECT COUNT(CASE WHEN Status = 'Pending' THEN 1 ELSE NULL END)
FROM tblData WHERE UID = 338559
对于VBA Access 2007
SELECT COUNT(IIF(Status = 'Pending', 1, NULL))
FROM tblData WHERE UID = 338559
答案 2 :(得分:0)
你不必数数,只需循环:
query = "SELECT COUNT Status = 'Pending' FROM tblData WHERE UID = 338559"
Set db = CurrentDb
Set rs = db.OpenRecordset(query)
If rs.RecordCount > 0 Then
While rs.EOF = False
'code to display it to listbox
rs.MoveNext
Wend
End If
rs.Close
Set rs = Nothing
Set db = Nothing
答案 3 :(得分:0)
对于记录:要使用查询中的数据填充列表框,您既不需要Count()
也不需要VBA。
只需将列表框RowSource
设置为查询,例如
SELECT <whichever fields you want to display> FROM tblData
WHERE UID = '338559' AND Status = 'Pending';