I'm trying to iterate through the result set generated with the query that has 2 parameters. Values for these parameters are read from the form fields (start and end date). Since Access throws Run-time error '3061'. Too few parameters. Expected 2. even if the value is set in the form fields, I tried to set the parameters through VBA with QueryDef object (given code below).
It worked OK when start and end date are the same, but if I select different start and end date it won't apply the date filter assigned to the query parameters.
I've tried both to change format of the date values and to cast them to another type, but I've had no success.
Has anyone experienced a similar problem? Any help would be appreciated!
Query:
SELECT DISTINCT
tblComp_Payout.Agent_ID_int As [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM
tblExchOffices
INNER JOIN
tblComp_Payout ON tblExchOffices.Agent_ID_int = tblComp_Payout.Agent_ID_int
WHERE
((DateValue([Paid_Date])) >= ([forms]![frmReporting]![txtDateFrom])
AND (DateValue([Paid_Date]))<=[forms]![frmReporting]![txtDateTo])
UNION
SELECT DISTINCT
tblComp_Sending.Agent_ID_int AS [Agent ID],
tblExchOffices.Agent_Name AS Name
FROM
tblExchOffices
INNER JOIN
tblComp_Sending ON tblExchOffices.Agent_ID_int = tblComp_Sending.Agent_ID_int
WHERE
((DateValue([Sending_Date])) >= ([forms]![frmReporting]![txtDateFrom])
AND (DateValue([Sending_Date]))<=[forms]![frmReporting]![txtDateTo]);
Method:
Private Sub iterate_Click()
On Error GoTo iterate_Err
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("queAgentByDate")
qdf.Parameters.Refresh
If CurrentProject.AllForms("frmReporting").IsLoaded Then
qdf.Parameters("[forms]![frmReporting]![txtDateFrom]") = CStr([Forms]![frmReporting]![txtDateFrom])
qdf.Parameters("[forms]![frmReporting]![txtDateTo]") = CStr([Forms]![frmReporting]![txtDateTo])
Else
Beep
Resume iterate_Exit
End If
Set rs = qdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
MsgBox rs.RecordCount
If rs.EOF Then Exit Sub
With rs
Do Until .EOF
'Loop logic
Loop
End With
rs.Close
Set rs = Nothing
iterate_Exit:
Exit Sub
iterate_Err:
MsgBox Error$
Resume iterate_Exit
End Sub