vba - 尝试使用 VBA 在 Access 中打开记录集时出现 "Too few parameters error"

标签 vba ms-access parameters ms-access-2016

我找到了another answer on here这解决了这个问题,但对我没有帮助。我检查了我尝试引用的查询,没有发现任何字段有问题。我还尝试了如何声明和设置对象和内容,但这也不起作用。

Dim dbsCurrent As DAO.Database
Dim rst As DAO.Recordset

Set dbsCurrent = CurrentDb
Set qdf = CurrentDb.QueryDefs("qry_FilmZip")
Set rst = qdf.OpenRecordset 'The error points to this line

rad_full = rst!radius_full

MsgBox ("rad_full:" + rad_full)

更新:我尝试为 .OpenRecordSet 方法提供如下查询名称:Set rst = qdf.OpenRecordset("qry_FilmZip")

...但现在它给了我一个新错误:运行时错误 3421:数据类型转换错误。有人知道发生了什么事吗?错误指向同一行。

我找到了如何解决第二个错误。事实证明我必须这样做

For Each prm In qdf.Parameters
    prm.Value = Eval(prm.Name)
Next prm

...但我不明白这到底是做什么的。有人可以启发我吗?

SQL:

SELECT 

  tbl_FilmZipInfo.ID, 
  tbl_FilmZipInfo.item, 
  tbl_FilmZipInfo.qty_per_unit, 
  tbl_FilmZipInfo.unit_of_measure, 
  tbl_FilmZipInfo.radius_core, 
  tbl_FilmZipInfo.radius_full, 
  tbl_FilmZipInfo.Lf_value_for_zipper, 
  tbl_FilmZipInfo.S_value_for_zipper, 
  tbl_FilmZipInfo.film_or_zip, 
  tbl_FilmZipInfo.Comments, 
  tbl_FilmZipInfo.physical_description

FROM 

  tbl_FilmZipInfo

WHERE 

  (((tbl_FilmZipInfo.item)=[Forms]![frm_FilmZip]![Text314]));

最佳答案

来源:https://msdn.microsoft.com/en-us/library/office/ff820966.aspx

此处唯一必需的参数是要打开的记录集的名称。

expression .OpenRecordset(Name, Type, Options, LockEdit)

在像您这样的参数查询中,您需要使用以下语法:

Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef 'You don't dim your qdf
Dim rst As DAO.Recordset

Set dbs = CurrentDb

'Get the parameter query
Set qfd = dbs.QueryDefs("qryMyParameterQuery")

'Supply the parameter value
qdf.Parameters("EnterStartDate") = Date
qdf.Parameters("EnterEndDate") = Date + 7

'Open a Recordset based on the parameter query
Set rst = qdf.OpenRecordset() 'Note the brackets here

您还可以在此处尝试以下类型:

Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset

Set dbs = CurrentDb

'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("Table1", dbOpenTable)

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

关于vba - 尝试使用 VBA 在 Access 中打开记录集时出现 "Too few parameters error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42117017/

相关文章:

java - 传递 Supplier<Stream<T>> 参数时 Stream.generate 方法的行为

sql-server - 从文本移动到 varchar(MAX) : Are there any troubles to expect with MS Access?

XSLT 与 XProc - 所需类型中的参数绑定(bind)

vba - VBA 中的 StringFromIID - 避免手动管理内存的好方法是什么?

vba - 使用VBA从PowerPoint演示文稿中提取评论

sql - 将记录永久标记为只读的最佳方法是什么?

ms-access - Access.Application.Eval() 的奇怪行为

android - Android Studio 中的自动填充功能参数

regex - 如何在 VBA 中使用 RegExp 隔离空格(\s 与\p{Zs})?

vba - VBA自定义函数可以知道公式是作为数组公式输入的吗?