“Too few parameters error” when trying to open a recordset in Access with VBA

百般思念 提交于 2019-12-13 16:35:57

问题


I found another answer on here that addressed this but it didn't help me. I checked the query I am trying to reference and I don't see a problem with any field. I also played around with how I declared and set the objects and stuff but that didn't work either.

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)

Update: I tried giving the .OpenRecordSet method the name of the query like this: Set rst = qdf.OpenRecordset("qry_FilmZip")

...but now it gives me a new error: Run-time error 3421: Data type conversion error. Anyone know what is going on? The error points to the same line.

I found out how to resolve the 2nd error. It turns out that I had to do

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

...but I don't understand what this is really doing. Could someone enlighten me?

The 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:


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

The only required argument here is the name of the recordset that you want to open.

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

In a parameter query like yours, you need to use the following syntax:

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

You could also try the following type 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)


来源:https://stackoverflow.com/questions/42117017/too-few-parameters-error-when-trying-to-open-a-recordset-in-access-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!