简体   繁体   English

基于多个条件的表中的MS ACCESS计数记录

[英]MS ACCESS Counting Records from table based on multiple criteria

I want a text box in Access to update with the number of records found where a manager has an "overdue" record in the table dependent on the manager selected in a combo box, I have the following code but am getting an error: 我希望Access中的一个文本框更新记录的数量,该数量取决于在组合框中选择的管理器中某个管理器在表中具有“过期”记录的管理器,我有以下代码,但出现错误:

Private Sub Combo26_AfterUpdate()

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT Advisor FROM tbltargets WHERE manager = '" & Me.Combo26.Column(0) & "' AND overdue = 'Overdue'")
If rs.EOF Then
Me.Text35 = 0
MsgBox "no overdue records", vbOKOnly
Else
rs.MoveLast
Me.Text35 = rs.RecordCount
rs.Close
End If

End Sub

When I select an option from the combo box it gives me an error "Too few parameters. Expected 1"... 当我从组合框中选择一个选项时,它给我一个错误“参数太少。预期为1” ...

I have similar code which works so am unsure why the above is resulting in an error, could anyone help? 我有类似的代码可以正常工作,所以不确定上面的代码为什么会导致错误,有人可以帮忙吗?

Thank you, 谢谢,

Check like this 像这样检查

Private Sub Combo26_AfterUpdate()

Dim rs As DAO.Recordset


If IsNull(Combo26.value) Then Exit Sub

Set rs = CurrentDb.OpenRecordset("SELECT COUNT(*) AS CNT FROM tbltargets WHERE manager = '" & Combo26.value & "' AND overdue = 'Overdue'")

If Not rs.BOF Then

    If Nz(rs!CNT, 0) = 0 Then
        Me.Text35 = 0
        MsgBox "no overdue records", vbOKOnly
    Else
        Me.Text35 = rs!CNT
    End If
End If

End Sub
  1. Used combobox.value instead of combobox.column 使用combobox.value而不是combobox.column
  2. Changed the SQL to COUNT(*) and adapted the code accordingly to improve perfs. 将SQL更改为COUNT(*)并相应地修改了代码以提高性能。
  3. Added some protection against NULL values (you should really add an Error Handler in this sub) 添加了一些针对NULL值的保护(您应该在此子项中确实添加错误处理程序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MS ACCESS-根据另一个字段的条件从表中动态检索记录 - MS ACCESS - Dynamically retrieve records from table based on criteria from another field MS Access,根据多个条件表单输入选择记录 - MS Access, Selecting records based on multiple criteria form inputs 使用MySQL中另一个表的多个条件对不同的记录进行计数 - Counting Distinct Records Using Multiple Criteria From Another Table In MySQL MS Access:根据另一个表字段的多条记录从一个表中选择记录 - MS Access: Selecting records from 1 table based on multiple records of another table's field 基于复杂标准的辅助表中的记录计数问题 - Issues with counting records in secondary table based on complex criteria MS-Access-查询以根据条件返回记录的每个子集 - MS-Access - Query to return each subset of records based on criteria 有没有办法根据两个条件过滤 ms-Access 记录? - Is there a way to filter ms-Access records based on two criteria? 如果查询 1 条件 1 的部分结果(访问),则 SQL 根据条件 2 获取表 1 中的所有记录 - SQL Get all records from table 1 based on criteria 2 if part of result of Query 1 Criteria 1 (Access) SQL-基于多个条件的计数 - SQL - counting based on multiple criteria 基于另一个表的Microsoft Access多个条件 - Microsoft Access Multiple Criteria based on another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM