Data Conversion error 3421 on OpenRecordset method

2020-04-19 20:04发布

问题:

I want to pass one parameter to a saved query in MS Access 2010 and then also get the results from the query: its my saved query:

SELECT Count(*)
FROM tb_KonzeptDaten
WHERE ( Konzept=[fzg_ID] and (DCMFile is null or (DCMFile='')));

and here is my VBA code to invoke this query:

Dim db As DAO.Database
Dim qry As DAO.QueryDef
Set qry = db.QueryDefs("Test_qr_emptyDCM")
qry.Parameters("fzg_ID").Value = ID
Set rs = qry.OpenRecordset("Test_qr_emptyDCM")

also the Type of ID in VBA code is Long and the field of Konzept is Database is Long integer Why do I get this error and how can I solve it?

回答1:

Your problem is with the qry.OpenRecordset statement. The first parameter for QueryDef.OpenRecordset is [Type] (e.g., dbOpenSnapshot). You don't need to provide the query name because you already supplied that when you created the QueryDef object.

Try using just

Set rs = qry.OpenRecordset