Share via

Problem with TempVars

Anonymous
2017-02-07T23:24:01+00:00

I am attempting to create the recordset. But I get a Runtime error 3061, Too few parameters. Expected 3

If I replace the tempVars in my query, with a regular parameter, say replace [tempVars]![CalendarID] with 1 and replace the tempVars fort year start and end with dates, everything works as expected.

Any guidance would be greatly appreciated.

This is the recordset:

    strSQL = "SELECT RequestDate, AvailableSlots " & _

             "FROM qry_FilteredSlots GROUP BY RequestDate, AvailableSlots " & _

             "HAVING Month([RequestDate]) = " & intMonth

        Set db = CurrentDb

        Set rst = db.OpenRecordset(strSQL)

        If rst.RecordCount > 0 Then rst.MoveFirst

This is the query:

SELECT qry_Quota_Assignments_ByDate.Dates AS RequestDate, qry_Filtered_RequestCount.CountOfRequestID, qry_Quota_Assignments_ByDate.Quota AS Slots, qry_Quota_Assignments_ByDate.CalendarID, IIf([CountOfRequestID]>=[Slots],0,1) AS AvailableSlots

FROM qry_Filtered_RequestCount INNER JOIN qry_Quota_Assignments_ByDate ON qry_Filtered_RequestCount.RequestDate = qry_Quota_Assignments_ByDate.Dates

GROUP BY qry_Quota_Assignments_ByDate.Dates, qry_Filtered_RequestCount.CountOfRequestID, qry_Quota_Assignments_ByDate.Quota, qry_Quota_Assignments_ByDate.CalendarID

HAVING (((qry_Quota_Assignments_ByDate.Dates) Between [tempVars]![YearStart] And [tempVars]![YearEnd]) AND ((qry_Quota_Assignments_ByDate.CalendarID)=[tempVars]![CalendarID]));

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2017-02-08T14:09:02+00:00

    Good. Then modify your code like this:

        strSQL = "SELECT RequestDate, AvailableSlots " & _

                 "FROM qry_FilteredSlots GROUP BY RequestDate, AvailableSlots " & _

                 "HAVING Month([RequestDate]) = " & intMonth

        Set db = CurrentDb

        Set qd = db.CreateQueryDef("", strSQL)

        For Each p In qd.Parameters

            p.Value = Eval(p.Name)

        Next p

        Set rst = qd.OpenRecordset(dbOpenSnapshot)

        If rst.RecordCount > 0 Then rst.MoveFirst

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-02-08T13:56:28+00:00

    You are correct

    Was this answer helpful?

    0 comments No comments
  3. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2017-02-08T03:12:54+00:00

    I always debug this the same way, along these lines. In my DebugUtilities module I have this procedure:

    Public Sub dumpParameters(ByVal strQueryName As String)

        Dim qd As DAO.QueryDef

        Dim p As DAO.Parameter

        Set qd = CurrentDb.QueryDefs(strQueryName)

        For Each p In qd.Parameters

            Debug.Print p.Name

        Next p

        Set qd = Nothing

    End Sub

    Then in the Immediate window I enter:

    dumpParameters "myQuery"

    Chances are you will get output like this:

    [tempVars]![YearStart]

    [tempVars]![YearEnd]

    Can you confirm?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-02-08T01:38:40+00:00

    The query is referenced in the FROM statement

    strSQL = "SELECT RequestDate, AvailableSlots " & _

                 "FROM qry_FilteredSlots GROUP BY RequestDate, AvailableSlots " & _

                 "HAVING Month([RequestDate]) = " & intMonth

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2017-02-08T00:36:54+00:00

    How exactly is the query involved in the RecordSet?  The rst is using the strSQL, not the query.  Please explain further.

    You should probably read: http://www.devhut.net/2011/11/07/ms-access-vba-run-parameter-query-in-vba/ to learn how to evaluate query parameters when executing queries through VBA.

    Was this answer helpful?

    0 comments No comments