"参数太少错误"尝试使用VBA在Access中打开记录集时

时间:2017-02-08 15:20:49

标签: 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")

...但现在它给了我一个新错误: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]));

1 个答案:

答案 0 :(得分:2)

来源: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)