vba 使用 QueryDefs 返回 Recordset 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11104728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:32:10  来源:igfitidea点击:

Error when using QueryDefs to return Recordset

ms-accessvbams-access-2007access-vba

提问by Michael T

I'm trying to access a query using VBA code. I think I've done it this way 100 times. My code (on a command button) starts like this:

我正在尝试使用 VBA 代码访问查询。我想我已经这样做了 100 次。我的代码(在命令按钮上)是这样开始的:

Dim rstDocCount As Recordset

Set rstDocCount = CurrentDb.QueryDefs("DocCount").OpenRecordset

rst.MoveFirst

I get this error message:

我收到此错误消息:

Run-time error '3061': Too few parameters. Expected 1.

运行时错误“3061”:参数太少。预期 1。

The Set rstDocCountline is highlighted yellow.

Set rstDocCount行以黄色突出显示。

What am I doing wrong? The only parameter should be the name of the query and I clearly have that.

我究竟做错了什么?唯一的参数应该是查询的名称,我清楚地知道。

回答by Fionnuala

You need:

你需要:

Dim rstDocCount As Recordset
Dim qdf As QueryDef

    Set qdf = CurrentDb.QueryDefs("DocCount")

    qdf.Parameters("Forms!Boxing!BoxID") = Forms!Boxing!BoxID 
    Set rstDocCount = qdf.OpenRecordset

    rstDocCount.MoveFirst

回答by HansUp

"The only parameter should be the name of the query and I clearly have that."

唯一的参数应该是查询的名称,我清楚地知道。

The OpenRecordsetmethod accepts 3 parameters: Type; Options; and LockEdit. However all 3 of those parameters are optional, so the "Too few parameters" error isn't about OpenRecordsetparameters.

OpenRecordset方法接受3个参数:Type; Options; 和LockEdit。然而,所有这三个参数都是可选的,因此“参数太少”错误与OpenRecordset参数无关。

Instead, as Remou pointed out, your QueryDefincludes something (frequently a field expression) the db engine can't find in the query's source table. In that situation, it treats the missing somethingas a parameter and requires a value for that parameter.

相反,正如 Remou 指出的那样,您QueryDef包含了数据库引擎在查询的源表中找不到的内容(通常是字段表达式)。在这种情况下,它将丢失的东西视为参数,并需要该参数的值。