help with DateAdd function

icyrius

New Member
Joined
Dec 22, 2012
Messages
15
I populated a criteria using the wizard to filter a query using a date range based on a date from a table, but it is not working.

Between DateAdd("d",+1,[tbl_OZ]![pilotDate]) And DateAdd("d",+2,[tbl_OZ]![pilotDate])

if I type the same formula using dates in this format #02/03/2016# then I get data.

What am I doing wrong? Thanks for your help.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Does the pilotDate field include time as well as date?
 
Upvote 0
Data is being pulled from two tables. Let me list the inputs the same way as Access,
Field: Date_from_Table2 <---- Includes time
Table: Table2
Sort: ascending
Show: checked
Criteria: Between DateAdd("d",+1,[table1]![pilotDate]) And DateAdd("d",+2,[table1]![pilotDate]) <---- does not include time
 
Last edited:
Upvote 0
Is this used in a Query? Can you post the SQL string of the Query?
Is table1 (or tbl_OZ) part of this query? Are all links established?

Try to add the two DateAdd functions as Fields in the Query and check the result. And maybe add [table1]![pilotDate] as a Field.
 
Upvote 0
1. Is this used in a Query? Yes.
2. Can you post the SQL string of the Query?
Code:
SELECT SALES.Facility_ID
    , [table1].pilotDate
    , SALES.Sales_Trans_NO
    , SALES.Sales_Trans_TM
FROM [table1] INNER JOIN SALES ON ([table1].pilotDate = SALES.Sales_Trans_DT) AND ([table1].StoreID = SALES.Location_ID)
WHERE (((SALES.Location_ID)=11) 
AND ((SALES.Sales_Trans_NO) Between DateAdd("d",+1,[table1]![pilotDate]) And DateAdd("d",+2,[table1]![pilotDate])));


3. Is table1 (or tbl_OZ) part of this query? Yes.
4. Are all links established? Yes.

5. Try to add the two DateAdd functions as Fields in the Query and check the result. Most functions work OK when used in fields. I am trying to use DateAdd inside the criteria. I works fine when not referencing a table/date.

6. And maybe add [table1]![pilotDate] as a Field. I tried this already and still does not work.
 
Upvote 0
If you can get results from something like Between #02/15/2016# And #03/01/2016
I'd say either the DateAdd function is not returning valid dates, the values being passed to it are not suitable, or it doesn't like your use of the bang operator (!).

If this is of no help
Code:
Between DateAdd("d",+1,[table1][COLOR=#ff0000][B].[/B][/COLOR][pilotDate]) And DateAdd("d",+2,[table1][B][COLOR=#ff0000].[/COLOR][/B][pilotDate])));

I would try variations of your query (in a copy of it) and include pilotDate as a field with no criteria. If you get records, ensure there are valid dates in this field (not NULL) and are formatted suitably for your system date/time settings. Then I'd add two more calculated fields to the new query to see what the function does to those valid dates:
Code:
Exp1: DateAdd("d",+1,[table1].[pilotDate]
and
Code:
Exp2:DateAdd("d",+2,[table1].[pilotDate]
and check the results. I don't think the fact that the field contains time is of any consequence for your issue, given that a static date works for you. However, you should know that using Between on date fields with time introduces a problem. When no time is provided, the second date cutoff point is midnight. That is, you will get no records for 1:00 AM (for example) for the second date. To compensate for this, one way is to DateAdd 23 hours and 59 minutes to your second date.
 
Upvote 0

Forum statistics

Threads
1,221,792
Messages
6,161,995
Members
451,735
Latest member
Deasejm

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