【问题标题】:"Too few parameters error" when trying to open a recordset in Access with VBA尝试使用 VBA 在 Access 中打开记录集时出现“参数太少错误”
【发布时间】:2017-06-26 07:13:15
【问题描述】:

我发现 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")

...但现在它给了我一个新错误:Run-time error 3421: Data type conversion error。有谁知道发生了什么?错误指向同一行。

我发现了如何解决第二个错误。原来我不得不这样做

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]));

【问题讨论】:

  • @Tom 指出的是Set rst = qdf.OpenRecordset()
  • '参数太少'错误指出查询期望它没有收到的参数。我建议您发布 SQL,这就是预期参数的位置...
  • @marlan 添加了 SQL
  • 假设tbl_FilmZipInfo 中的所有字段都确实存在并且拼写正确,请确保已加载frm_FilmZip,并且该字段Text314 拼写正确并且具有值。

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


【解决方案1】:

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

这里唯一需要的参数是您要打开的记录集的名称。

表达式 .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)

【讨论】:

  • 我正在使用的已保存查询不提示输入参数。对于其中一个字段,我使用“Cirteria”字段(在设计视图中)从表单中获取一个值。这和参数一样吗?
  • 如果查询不能访问表单中的值,它将把字段当作参数。是否已加载此表单? in 应该是什么值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多