Michael T
Michael T

Reputation: 1965

Error when using QueryDefs to return Recordset

I'm trying to access a query using VBA code. I think I've done it this way 100 times. My code (on a command button) starts like this:

Dim rstDocCount As Recordset

Set rstDocCount = CurrentDb.QueryDefs("DocCount").OpenRecordset

rst.MoveFirst

I get this error message:

Run-time error '3061': Too few parameters. Expected 1.

The Set rstDocCount line is highlighted yellow.

What am I doing wrong? The only parameter should be the name of the query and I clearly have that.

Upvotes: 5

Views: 6986

Answers (2)

HansUp
HansUp

Reputation: 97131

"The only parameter should be the name of the query and I clearly have that."

The OpenRecordset method accepts 3 parameters: Type; Options; and LockEdit. However all 3 of those parameters are optional, so the "Too few parameters" error isn't about OpenRecordset parameters.

Instead, as Remou pointed out, your QueryDef includes something (frequently a field expression) the db engine can't find in the query's source table. In that situation, it treats the missing something as a parameter and requires a value for that parameter.

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91376

You need:

Dim rstDocCount As Recordset
Dim qdf As QueryDef

    Set qdf = CurrentDb.QueryDefs("DocCount")

    qdf.Parameters("Forms!Boxing!BoxID") = Forms!Boxing!BoxID 
    Set rstDocCount = qdf.OpenRecordset

    rstDocCount.MoveFirst

Upvotes: 7

Related Questions