andybeanland
IS-IT--Management
Hello, I'm having a problem with a simple query.
In the Query Builder in Access I have created a query to count the number of records between certain dates.
When run this query, inputing the dates 01/01/05 and 01/02/05 when prompted it returns the correct result - 6 records.
However, I really want to open a recordset so I can pass this value to control in another form, so I've created the following code which is executed on the Event OnClick of a button.
The recordset opens but the MsgBox rs("NoOfDayCarePacks") returns 0. I have done a Debug.Print of the SQL code and it gives the following:
Can anyone spot my stupid mistake? or suggest a better way to do this?
Thanks for you help,
Andy
In the Query Builder in Access I have created a query to count the number of records between certain dates.
Code:
SELECT Count([tbl_PacksSent].[DayCare]) AS NoOfDayCarePacks
FROM tbl_PacksSent
WHERE ((([tbl_PacksSent].[DayCare])=-1) And (([tbl_PacksSent].[DateSent]) Between [Forms]![frm_DateSelectorNoOfPacks].[DateTo] And [Forms]![frm_DateSelectorNoOfPacks].[DateFrom]))
When run this query, inputing the dates 01/01/05 and 01/02/05 when prompted it returns the correct result - 6 records.
However, I really want to open a recordset so I can pass this value to control in another form, so I've created the following code which is executed on the Event OnClick of a button.
Code:
DateTo = [Forms]!frm_DateSelectorNoOfPacks.DateTo
DateFrom = [Forms]!frm_DateSelectorNoOfPacks.DateFrom
MsgBox DateTo
MsgBox DateFrom
SQLDayCare = "SELECT Count([tbl_PacksSent].[DayCare]) AS NoOfDayCarePacks FROM tbl_PacksSent WHERE ((([tbl_PacksSent].[DayCare])=-1) And (([tbl_PacksSent].[DateSent]) Between " & DateTo & " And " & DateFrom & "))"
'Defining the connection
Set conDatabase = CurrentProject.Connection
'Opening the recordset
Set rs = New Recordset
rs.Open SQLDayCare, conDatabase, 3, 3
MsgBox "Recordset open"
MsgBox rs("NoOfDayCarePacks")
The recordset opens but the MsgBox rs("NoOfDayCarePacks") returns 0. I have done a Debug.Print of the SQL code and it gives the following:
Code:
SELECT Count([tbl_PacksSent].[DayCare]) AS NoOfDayCarePacks FROM tbl_PacksSent WHERE ((([tbl_PacksSent].[DayCare])=-1) And (([tbl_PacksSent].[DateSent]) Between 01/01/2005 And 01/02/2005))
Can anyone spot my stupid mistake? or suggest a better way to do this?
Thanks for you help,
Andy