Error when using QueryDefs to return Recordset
Asked Answered
U

2

5

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.

Unification answered 19/6, 2012 at 15:52 Comment(3)
Open the query in the database window. It will show you which field is mispelled or which parameter is missing.Quantifier
There's no problem with the query. I am able to open it while the form is open, from which this code is running. The query has a parameter Forms!Boxing!BoxID (Boxing is the current form). My suspicion is that vba can't read that field for some reason. I found that if I replace the query with other queries that use a parameter from the Boxing form, I get the same error. If I replace it with a simpler query there is no problem. So I think I need to instead make the query show every record, then loop through them to get to the one that matches the BoxID on my form.Unification
You just need to set the parameter. See below.Quantifier
Q
7

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
Quantifier answered 19/6, 2012 at 17:25 Comment(0)
S
1

"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.

Spigot answered 19/6, 2012 at 16:40 Comment(1)
See my comment above replying to Remou.Unification

© 2022 - 2024 — McMap. All rights reserved.