Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select dates based on an expression

Status
Not open for further replies.
Sep 2, 2002
32
DE
Hi Everybody,

I have database in which I create an expression within a query.
Real Date: IIf([ShiftPat]="Night",[CDate]-1,[CDate])

It supposed to change dates to match a shift pattern, which it does fine. I then create another query based on this, in which I want to select all data between two dates. But the query seems to treat them like text and not dates and brings in some very strange dates.

Is their anyway I can tell access that this is a date and not a text string. I have tried using DateValue but it doesn't seem to make any difference.

Anybody got any ideas.

Thanks
 

Try changing your expression to this:
Code:
Real Date: IIf([ShiftPat]="Night",Date()-1,Date())
Let me know if this helps...
 
Sorry for the confusion, perhaps I should explain the code a little bit better, the CDate isn't actually the current date function of Access, it's the name I called the field in the table. So it's looking up a date and taking one away from it.
 
CDate also happens to be the name of a function in Access. You might want to change the name of this control.

To subtract one day from a date field, try DateAdd:
Code:
DateAdd("d", -1, [CDate])
will subtract one day from CDate so:
Code:
IIf([ShiftPat]="Night",DateAdd("d", -1, [CDate]),[CDate])
 
Tried that but no luck, it does the same thing as my first expression, in that it gives me the information I was after but also gives me other dates. For example if I select 14/05/2003 - 17/05/2003, I will get all those dates plus others like 14/03/2003 and 15/03/2003.

Which I believe is because it's seeing the date as a string expression when looking up the information in the criteria.
 
Just as I side note,

I noticed that critera "between #15/05/2003# and #18/05/2003#" works ok, but not "between [Enter first date] and [Enter last date]". Is their away to put the # sign in whilst still allowing the user to input the dates on the query?
 
OK, maybe there is a conflict with your regional settings. Try enforcing the format with the parameters. Something like this:
Code:
Between Format([Enter first date],"dd/mm/yyyy") And Format([Enter last date],"dd/mm/yyyy")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top