Using DateValue in query (1 Viewer)

Angel69

Registered User.
Local time
Today, 17:21
Joined
Jun 11, 2013
Messages
86
Hi,

How do I use the DateValue function with a query that pulls data between dates? Here is my code below. I'm getting an error that my expression is too complex or incorrect.

Code:
SELECT IIf([tblAUCodes].[BusinessLine] Like '*CMES*',"CMES",IIf([tblAUCodes].[BusinessLine] Like '*HOUS*',"CMES",IIf([tblAUCodes].[BusinessLine] Like 'CTO',"CTO",IIf([tblAUCodes].[BusinessLine] Like 'Shareowner*',"WFSS","SPS")))) AS LOB, Count(tblPACSTemplate.temID) AS CountOftemID, tblPACSTemplate.Type, DateValue([ApprovedDate]) AS ApproveDate
FROM tblPACSTemplate LEFT JOIN tblAUCodes ON tblPACSTemplate.[OpenByAU] = tblAUCodes.AU
GROUP BY IIf([tblAUCodes].[BusinessLine] Like '*CMES*',"CMES",IIf([tblAUCodes].[BusinessLine] Like '*HOUS*',"CMES",IIf([tblAUCodes].[BusinessLine] Like 'CTO',"CTO",IIf([tblAUCodes].[BusinessLine] Like 'Shareowner*',"WFSS","SPS")))), tblPACSTemplate.Type, DateValue([ApprovedDate])
HAVING (((DateValue([ApprovedDate])) Between [Forms]![Main Menu]![StartDate] And [Forms]![Main Menu]![EndDate]))
ORDER BY IIf([tblAUCodes].[BusinessLine] Like '*CMES*',"CMES",IIf([tblAUCodes].[BusinessLine] Like '*HOUS*',"CMES",IIf([tblAUCodes].[BusinessLine] Like 'CTO',"CTO",IIf([tblAUCodes].[BusinessLine] Like 'Shareowner*',"WFSS","SPS"))));

TIA
 

vbaInet

AWF VIP
Local time
Today, 22:21
Joined
Jan 22, 2010
Messages
26,374
It's a real Date/Time field right? You don't need DateValue().
And it should be in the WHERE clause not HAVING.
 

Angel69

Registered User.
Local time
Today, 17:21
Joined
Jun 11, 2013
Messages
86
I need the DateValue because when I select the date range for exampel 6/22/14 - 6/30/14 none of my 6/30/14 items show up I'm guessing because the time is later in the day. I use the DateValue to strip the time.
 

Angel69

Registered User.
Local time
Today, 17:21
Joined
Jun 11, 2013
Messages
86
I'm getting the attached error when I change it to WHERE instead of HAVING.
 

Attachments

  • ERROR.png
    ERROR.png
    20.2 KB · Views: 75
Last edited:

vbaInet

AWF VIP
Local time
Today, 22:21
Joined
Jan 22, 2010
Messages
26,374
Create a query based on this one and add the WHERE criteria there.
 

vbaInet

AWF VIP
Local time
Today, 22:21
Joined
Jan 22, 2010
Messages
26,374
I'll expand on my last post.

Save this query (as qryTest for example):
Code:
SELECT IIf([tblAUCodes].[BusinessLine] Like '*CMES*',"CMES",
               IIf([tblAUCodes].[BusinessLine] Like '*HOUS*',"CMES",
                   IIf([tblAUCodes].[BusinessLine] Like 'CTO',"CTO",
                       IIf([tblAUCodes].[BusinessLine] Like 'Shareowner*',"WFSS","SPS")))) AS LOB, 
          Count(tblPACSTemplate.temID) AS CountOftemID, 
          tblPACSTemplate.Type, 
          DateValue([ApprovedDate]) AS ApproveDate
FROM tblPACSTemplate LEFT JOIN tblAUCodes ON tblPACSTemplate.[OpenByAU] = tblAUCodes.AU
GROUP BY IIf([tblAUCodes].[BusinessLine] Like '*CMES*',"CMES",
                    IIf([tblAUCodes].[BusinessLine] Like '*HOUS*',"CMES",
                        IIf([tblAUCodes].[BusinessLine] Like 'CTO',"CTO",
                            IIf([tblAUCodes].[BusinessLine] Like 'Shareowner*',"WFSS","SPS")))), 
                tblPACSTemplate.Type, 
                DateValue([ApprovedDate]);
Create a query based on qryTest and add the sort/criteria here:
Code:
WHERE [ApproveDate] Between [Forms]![Main Menu]![StartDate] And [Forms]![Main Menu]![EndDate]
ORDER BY LOB
 

Angel69

Registered User.
Local time
Today, 17:21
Joined
Jun 11, 2013
Messages
86
Thanks for taking a look at it but that does not seem to work either. I'm getting a Data type mismatch in criteria expression error.
 
Last edited:

vbaInet

AWF VIP
Local time
Today, 22:21
Joined
Jan 22, 2010
Messages
26,374
Where are you getting the data type mismatch error? Which query?
 

Users who are viewing this thread

Top Bottom