[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Access 2007 Simple Custom Date Function Returning Very Weird Results

Posted on 2010-01-08
Medium Priority
263 Views
Hello all,

I am working on a simple custom function (really simple) so I can test some different formulas to extract data based on dynamic date ranges. I have stuff that always fails in January because I haven't properly accounted for the change in years, so this year I'm trying to fix it once and for all.

I use a combination of Date() and DateSerial for most of the stuff I do, but I want to "simulate" "today" as some other date so I can test my formulas. So I created this simple function:

Function SimDate()
SimDate = #1/8/2010#
End Function

so that I could enter any date I wanted and see if the other formulas I've developed are working. But I get some results that I cannot explain:

[ First, assume that today is january 8th, 2010. Also, the date field being queried is mm/dd/yyy]

In this example, I am creating a formula to extract the current YTD up to 4 days ago. If today is 1/8/10, then I would expect it to return data from 1/1/10 - 1/4/10.

if I use this:

>=DateSerial(Year(Date()-4),1,1) And <=Date()-4

OR THIS:

>=DateSerial(Year(#1/8/2010#-4),1,1) And <=#1/8/2010#-4

it CORRECTLY returns 1/1-1/4 (assuming today is 1/8)

However, if I use the custom function:

Function SimDate()
SimDate = #1/8/2010#
End Function

and use it this way in the query:

>=DateSerial(Year(SimDate()-4),1,1) And <=SimDate()-4

It INCORRECTLY returns data between 1/1 - 1/6 (assuming today is 1/8)

Thanks in advance to anyone who can help me determine why I get different results using the function than if I hardcoded the same date value. In other words, why this:

>=DateSerial(Year(#1/8/2010#-4),1,1) And <=#1/8/2010#-4

returns different results than this:

Function SimDate()
SimDate = #1/8/2010#
End Function

and use it this way in the query:

>=DateSerial(Year(SimDate()-4),1,1) And <=SimDate()-4

Cheers!

0
Question by:Jim M

LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 800 total points
ID: 26214741
test

Function SimDate() as date
SimDate = #1/8/2010#
End Function
0

LVL 2

Author Comment

ID: 26214827
Thanks for the quick response...guess I needed to declare the data type...doh!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
###### Suggested Courses
Course of the Month18 days, 14 hours left to enroll