Created new date fields, but not able to use "between" with the query design

TAM

Board Regular
Joined
Oct 10, 2008
Messages
114
I created two date fields: StartDate and Enddate. I backtested each new date field and they calculated and displayed correctly when displaying the detailed transactions. However, when I try to summarize in the query design tools transactions between StartDate and Enddate, I get an error message: Data type mismatch in criteria expression. Here's the SQL:

SELECT [SALESREP_12-22-2015].MBRID, [SALESREP_12-22-2015].FileCreationDate, CDate([FileCreationDate]-362) AS StartDate, CDate([FileCreationDate]+1) AS EndDAte, dbo_TransactionHistory.ReportDate, dbo_TransactionHistory.WklyTotalGDC AS SY_SALES INTO Tbl_Metrics_Test
FROM dbo_TransactionHistory LEFT JOIN [SALESREP_12-22-2015] ON dbo_TransactionHistory.AdvisorNo = [SALESREP_12-22-2015].MBRID
WHERE ((([SALESREP_12-22-2015].MBRID)=12345) AND ((dbo_TransactionHistory.ReportDate) Between "StartDate" And "Enddate"));SALESREP

What am I missing?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You cannot use calculated field names (aliases) that you create in your SELECT clause in your WHERE clause. Just use the calculation itself, i.e.

SELECT [SALESREP_12-22-2015].MBRID, [SALESREP_12-22-2015].FileCreationDate, CDate([FileCreationDate]-362) AS StartDate, CDate([FileCreationDate]+1) AS EndDAte, dbo_TransactionHistory.ReportDate, dbo_TransactionHistory.WklyTotalGDC AS SY_SALES INTO Tbl_Metrics_Test

FROM dbo_TransactionHistory LEFT JOIN [SALESREP_12-22-2015] ON dbo_TransactionHistory.AdvisorNo = [SALESREP_12-22-2015].MBRID
WHERE ((([SALESREP_12-22-2015].MBRID)=12345) AND ((dbo_TransactionHistory.ReportDate) Between (CDate([FileCreationDate]-362)) And (CDate([FileCreationDate]+1))));
 
Upvote 0
Excellent! This may need to be a new post, but after I added the change, while the query executes, before it finished, I got a message "Invalid use of Null".

You cannot use calculated field names (aliases) that you create in your SELECT clause in your WHERE clause. Just use the calculation itself, i.e.

SELECT [SALESREP_12-22-2015].MBRID, [SALESREP_12-22-2015].FileCreationDate, CDate([FileCreationDate]-362) AS StartDate, CDate([FileCreationDate]+1) AS EndDAte, dbo_TransactionHistory.ReportDate, dbo_TransactionHistory.WklyTotalGDC AS SY_SALES INTO Tbl_Metrics_Test

FROM dbo_TransactionHistory LEFT JOIN [SALESREP_12-22-2015] ON dbo_TransactionHistory.AdvisorNo = [SALESREP_12-22-2015].MBRID
WHERE ((([SALESREP_12-22-2015].MBRID)=12345) AND ((dbo_TransactionHistory.ReportDate) Between (CDate([FileCreationDate]-362)) And (CDate([FileCreationDate]+1))));
 
Upvote 0
Do you have NULL values in any of the fields you are doing calculations on or joining on?
 
Upvote 0
You are welcome.

By the way, the NZ function is a nice tool for dealing with unwanted NULL values.
See: MS Access: Nz Function
 
Upvote 0
However, becareful when using NZ ... A) it is MS Access specific function and more importantly B) NZ on dates you will want to supply a default value (2nd parameter) otherwise you get a Zero ... i.e. 12/31/1899 !!! which is probably not what you want :cool:
 
Upvote 0
NZ on dates you will want to supply a default value (2nd parameter) otherwise you get a Zero
I always supply the second value. No need to leave it up to chance!
 
Upvote 0

Forum statistics

Threads
1,221,819
Messages
6,162,155
Members
451,749
Latest member
zack_ken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top