473,791 Members | 3,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

As Of Date function

I made a simple public function to set and return a date value (see
below). I have a number of queries that call up the function to get
the "As Of Date," which is typically set to today's date. Occasionally
though, I need to change the "As Of Date" to some date in the past,
and then when I run the queries, they're based on that date in the
past. This generally works fine, but, sometimes, the date gets reset
to something WAY in the past like "1/1/1900 12:00:00AM" (it's always
the same, I just can't recall the exact date).

Once that happens, the only way to get the date to reset to today's
date is to recompile the database. I checked everywhere I was setting
the date to ensure that I was using the right data type. No problems
there. Any ideas? (see code below) Thanks!

Option Compare Database
Option Explicit
Public dteAsOfDate As Date

Function SetAsOfDate(dte As Date)

dteAsOfDate = dte

End Function

Function GetAsOfDate() As Date

GetAsOfDate = dteAsOfDate

End Function
Oct 17 '08 #1
3 4414
Oops, not "recompile" the database. I meant "compact" the database.
Oct 17 '08 #2
Day zero of the Access date system was December 30, 1899. If Access looks at
a variable, and treats it as a string value rather than a date, it can treat
it as day zero, and hence the problem you describe.

The solution has to do with the way you pass and return dates to/from the
function. I would imagine that the simplest approach would be pass two
values into your function:
a) the value whose age you are trying to determine;
b) the 'as-of' date.
You could make (b) optional, and have the function use today if missing.

Another factor here is that the Date in VBA cannot handle Null. (That's true
of all VBA types except variant.) It's quite common for
queries/forms/reports to pass a null value to a function. Even if the field
is required, it can happen (e.g. outer joins, or even the primary key value
at a new record.) In general, this means the function needs to accept and
return a Variant, and the query needs to massage the returned value.

So, you might declare the function like this:
Function Age(varDate As Variant, Optional varAsOf As Variant) As Variant

Then in the query, to get the age based on the DOB field as of today, you
would type an expression like this in the Field row in query design:
Age([DOB])
Or to get the age as of Jan 1 next year:
Age([DOB], #1/1/2009#)

Example of such a function:
http://allenbrowne.com/func-08.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

<mu************ *@gmail.comwrot e in message
news:f7******** *************** ***********@f63 g2000hsf.google groups.com...
>I made a simple public function to set and return a date value (see
below). I have a number of queries that call up the function to get
the "As Of Date," which is typically set to today's date. Occasionally
though, I need to change the "As Of Date" to some date in the past,
and then when I run the queries, they're based on that date in the
past. This generally works fine, but, sometimes, the date gets reset
to something WAY in the past like "1/1/1900 12:00:00AM" (it's always
the same, I just can't recall the exact date).

Once that happens, the only way to get the date to reset to today's
date is to recompile the database. I checked everywhere I was setting
the date to ensure that I was using the right data type. No problems
there. Any ideas? (see code below) Thanks!

Option Compare Database
Option Explicit
Public dteAsOfDate As Date

Function SetAsOfDate(dte As Date)

dteAsOfDate = dte

End Function

Function GetAsOfDate() As Date

GetAsOfDate = dteAsOfDate

End Function
Oct 18 '08 #3
Perhaps you should check for a valid AsOfDate with the GetAsOfDate
Function.

e.g. (air code)

Dim mAsOfDate As Date

Public Function LetAsOfDate(Opt ional vDate As Date)
On Error GoTo AsOfDateErr
If CDbl(vDate) = 0 Then
vDate = CDate(InputBox( "Enter As Of Date in format yyyy-mm-
dd", "As Of Date", Format(VBA.Date (), "yyyy-mm-dd")))
End If
mAsOfDate = vDate
AsOfDateExit:
mAsOfDate = GetAsOfDate()
Exit Function
AsOfDateErr:
MsgBox Err.Description , vbInformation, "error " & Err.Number
Resume AsOfDateExit
End Function

Public Function GetAsOfDate() As Date
If CDbl(mAsOfDate) = 0 Then LetAsOfDate
GetAsOfDate = mAsOfDate
End Function

On Oct 17, 3:39*pm, murch.alexan... @gmail.com wrote:
I made a simple public function to set and return a date value (see
below). I have a number of queries that call up the function to get
the "As Of Date," which is typically set to today's date. Occasionally
though, I need to change the "As Of Date" to some date in the past,
and then when I run the queries, they're based on that date in the
past. This generally works fine, but, sometimes, the date gets reset
to something WAY in the past like "1/1/1900 12:00:00AM" (it's always
the same, I just can't recall the exact date).

Once that happens, the only way to get the date to reset to today's
date is to recompile the database. I checked everywhere I was setting
the date to ensure that I was using the right data type. No problems
there. Any ideas? (see code below) Thanks!

Option Compare Database
Option Explicit
Public dteAsOfDate As Date

Function SetAsOfDate(dte As Date)

* * dteAsOfDate = dte

End Function

Function GetAsOfDate() As Date

* * GetAsOfDate = dteAsOfDate

End Function
Oct 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5219
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much appreciated. TIA
8
2673
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them to select today. example: var dtToday = Date() if(document.frmSoftware.txtDDate.value == dtToday) {
30
3697
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
1
2807
by: Liz Malcolm | last post by:
Hello and TIA. I have a DE form with an option group that if daily is selected todays date is used for start and end date, if weekly is selected Monday - Friday is used. I am trying to add a manual date selection to clear the start and end date to allow the end user input other days. I have taken into account our financial 1st and last day of the month. I was able to break the option group into a select case, but I cannot get the date...
10
11970
by: John Morgan | last post by:
Does anyone know what parameter should be used instead of Date = 0 for the optional parameter in the following function? Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date = 0) As Short
12
29474
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
6
12559
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a calendar control to allow my users to enter a date but I need them to enter a time as well. It doesn't look like the calendar control will allow times to be entered so I was thinking of having two text boxes. One text box would contain the date...
3
3442
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the current date and time expressed in minutes? Once I have this number I can add the number of minutes purchased to the current date/time to get the expiration date.
10
2549
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the database. Is there any way to change value of this field back to yyyymmdd format as accepted correctly in sql. ? The change should preferably be when user click on submit.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.