cansevin
asked on
Query Date Field
I am trying to run a query from a query that has a list of dates. I want it only for the current day and it is not reading the WHERE properly, it pull up zero results. Below I have the query that pulls up zero result. This same query without the HAVING section works fine. I feel like the column isn't being read properly as a "date". Is there something I have to do to format that?
SELECT qryStatsforDailySalesRepor t.DateFiel d, qryStatsforDailySalesRepor t.Messages Recorded, qryStatsforDailySalesRepor t.TotalNew Clients, qryStatsforDailySalesRepor t.BookedTr ue, qryStatsforDailySalesRepor t.TalkedTo , qryStatsforDailySalesRepor t.PercentB ooked, qryStatsforDailySalesRepor t.FiveMinC allBack, qryStatsforDailySalesRepor t.Percenti nFive, qryStatsforDailySalesRepor t.AvgOfSum OfExtended Price, "CurrentDay" AS DateSpec
FROM qryStatsforDailySalesRepor t
GROUP BY qryStatsforDailySalesRepor t.DateFiel d, qryStatsforDailySalesRepor t.Messages Recorded, qryStatsforDailySalesRepor t.TotalNew Clients, qryStatsforDailySalesRepor t.BookedTr ue, qryStatsforDailySalesRepor t.TalkedTo , qryStatsforDailySalesRepor t.PercentB ooked, qryStatsforDailySalesRepor t.FiveMinC allBack, qryStatsforDailySalesRepor t.Percenti nFive, qryStatsforDailySalesRepor t.AvgOfSum OfExtended Price, "CurrentDay"
HAVING (((qryStatsforDailySalesRe port.DateF ield)=Date ()));
SELECT qryStatsforDailySalesRepor
FROM qryStatsforDailySalesRepor
GROUP BY qryStatsforDailySalesRepor
HAVING (((qryStatsforDailySalesRe
ASKER
Unfortunately still not working
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>I feel like the column isn't being read properly as a "date".
By any chance is column DateField a Text field, which you are applying date logic to?
If yes, what would happen if the column has a non-date value, such as blank, NULL, "banana"?
By any chance is column DateField a Text field, which you are applying date logic to?
If yes, what would happen if the column has a non-date value, such as blank, NULL, "banana"?
You probably have both date and time. Try removing time:
HAVING Int(qryStatsforDailySalesR eport.Date Field)=Dat e();
or (faster if DateField is indexed):
HAVING qryStatsforDailySalesRepor t.DateFiel d Between Date() And Date() + TimeSerial(23,11,59);
/gustav
HAVING Int(qryStatsforDailySalesR
or (faster if DateField is indexed):
HAVING qryStatsforDailySalesRepor
/gustav
What data type is the DateField field? If it isn't a Date field, you might need to convert its value to a Date variable, using CDate(), maybe after checking that the value in the field can be converted to a Date, using IsDate(). Here is some sample code that checks the value in a form control:
If IsDate(Me![txtFromDate].Value) = True Then
dteFromDate = CDate(Me![txtFromDate].Value)
End If
You can also use the DateValue() function to extract just the date from a datetime field.
Where DateTime(YourDate) = Date();
I would switch the Having to a Where. Where is evaluated BEFORE aggregation and Having is evaluated AFTER aggregation. Since the date isn't going to be affected by the Group By, it is more efficient to get rid of the data you don't need before you make the query engine go through the process of aggregating it. Having is only used when your criteria doesn't actually exist in the Select clause.
Where DateTime(YourDate) = Date();
I would switch the Having to a Where. Where is evaluated BEFORE aggregation and Having is evaluated AFTER aggregation. Since the date isn't going to be affected by the Group By, it is more efficient to get rid of the data you don't need before you make the query engine go through the process of aggregating it. Having is only used when your criteria doesn't actually exist in the Select clause.
I see it now. I generally don't look at posts that just say "try this" without any explanation of what/why.
Why use DateValue(Now()) rather than a simple Date()?
Why use DateValue(Now()) rather than a simple Date()?
Why use DateValue(Now()) rather than a simple Date()?
I have been bitten more than once where bad reference orders, localities, Win setup, VBA, etc. have given a syntax error or misinterpretation of what you are trying to do. But the Now() is more specific and the stripping back to the date only adds probably a 1/2MS to processing.
I have been bitten more than once where bad reference orders, localities, Win setup, VBA, etc. have given a syntax error or misinterpretation of what you are trying to do. But the Now() is more specific and the stripping back to the date only adds probably a 1/2MS to processing.
> Why use DateValue(Now()) rather than a simple Date()?
There is no reason. It adds nothing and is marginally slower.
Jim, it will only obfuscate code as any other developer who would happen to maintain the code will ask as Pat: Why did he do this?
As to the issue with references, troubles with Date() and Left() and other very basic functions are only symptoms, never the cause.
/gustav
There is no reason. It adds nothing and is marginally slower.
Jim, it will only obfuscate code as any other developer who would happen to maintain the code will ask as Pat: Why did he do this?
As to the issue with references, troubles with Date() and Left() and other very basic functions are only symptoms, never the cause.
/gustav
ASKER
Thanks! That was the one that worked! Have a great day
Open in new window