Queries and the 3061 "Too few parameters. Expected 1." message

Your query is working just fine referencing a form to select some records:

QueryFormParm

but when you run it in code you get the dreaded 3061 "Too few parameters. Expected 1." message.

One option is to evaluate the Forms![Form name]![Field Name] expression as per the following examples.

The below example is an execute query.

Set db = CurrentDb
Set qdf = db.QueryDefs("LEMBatchesRpt Append to temp table")
For i = 0 To qdf.Parameters.Count - 1
    qdf.Parameters(i) = Eval(qdf.Parameters(i).Name)
Next i

qdf.Execute dbFailOnError

The below example is used in my standard routine which copies the contents of a recordset into an Excel spreadsheet.  Due to the requirements of basing the openrecordset on a query to which I added a Where clause I created a temporary query and then did the parameter evaluation

strSQL = "SELECT * FROM [" & strQuery & "]"
If Len(strWhere) > 0 Then _
    strSQL = strSQL & " WHERE " & strWhere & ";"
Set qdf = db.CreateQueryDef("", strSQL)
For i = 0 To qdf.Parameters.Count - 1
    qdf.Parameters(i) = Eval(qdf.Parameters(i).Name)
Next i
Set rs = qdf.OpenRecordset(dbOpenSnapshot)

….

' Insert recordset into cells starting at left hand side and second row
'  Using a copyfromRecordset is much, much faster than stuffing in the cells one at a time.
.range("A2").CopyFromRecordset rs

This is used in a generic routine behind the very flexible reporting form in the Granite Fleet Manager.  When the user clicks on the Export Report to Excel command button I look at the record source of the report to fetch the query name.   I then build the where clause and execute the above code.

Published Tue, Sep 22 2009 22:32 by Tony
Filed under: , ,

Comments

# No teme

Hello from Russia!

Can I quote a post in your blog with the link to you?

Friday, October 23, 2009 5:16 AM by Polprav

# re: Queries and the 3061 "Too few parameters. Expected 1." message

Polprav

Yes, you may quote a sentence or a paragraph from any blog posting or website as well as include a link.   But not the entire page.

Also note that you are possibly a blog spammer so I removed your URL from your comment.

Saturday, October 24, 2009 4:43 PM by Tony

Leave a Comment

(required) 
(required) 
(optional)
(required)