访问VBA:打开记录集要求4个参数

Access VBA: Opening the recordset asks for 4 parameters

Private Sub Command22_Click()

    ' This section deals with grabbing the 3 calculations from qry_LiveOEE
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    Set dbs = CurrentDb

    'Open a dynaset-type Recordset using a saved query
    Set rst = dbs.OpenRecordset("qry_LiveOEE", dbOpenDynaset)

    rst.MoveFirst
    numfg_posted = rst!SumOfqty_complete
    numOEE = rst!OEE
    numpp_lhr = rst!ACT_PPLHR

    rst.Close
    dbs.Close

    Set rst = Nothing
    Set dbs = Nothing

我收到一条错误消息说 Too few parameters. Expected 4. 此查询在 criteria 部分(设计视图)中有 5 个内容,所以为什么说我需要 4 个参数?

条件部分中的 5 件事(都在不同的字段下)是:

  1. 从表单输入
  2. 从表单输入
  3. 根据当前时间切换语句
  4. 日期()
  5. 不为空

试试这个(未测试)更新代码:

Private Sub Command22_Click()

    ' This section deals with grabbing the 3 calculations from qry_LiveOEE
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim qdf AS DAO.QueryDef
    Dim prm As DAO.Parameter

    Set dbs = CurrentDb

    Set qdf = dbs.QueryDefs("qry_LiveOEE")
    For Each prm In qdf.Parameters
        prm.Value = Eval(prm.Name)
    Next prm
    Set rst = qdf.OpenRecordset

    rst.MoveFirst
    numfg_posted = rst!SumOfqty_complete
    numOEE = rst!OEE
    numpp_lhr = rst!ACT_PPLHR

    rst.Close
    dbs.Close

    Set rst = Nothing
    Set dbs = Nothing

确保参数中的所有值都可用 - 即表单已打开。