Solved

If Date is Null Then Date = Date()

Posted on 2013-06-10
4
500 Views
Last Modified: 2013-06-10
I have found a function that functions almost to my requirements and is used to show the years of service for an employee. The problem is that it requires a start date and end date. (Both fields on a form) Therefore if an employee is still in service then there is no enddate, so therefore if the enddate is null I would like it to take todays Date() as the enddate

The form also contains a field with the expression

=YMD_DateDiff([StartDate],[EndDate])

The function behind the form is

Function YMD_DateDiff(ByVal pdStDate As Date, _
ByVal pdEndDate As Date) As String
Dim iYears As Integer, iMonths As Integer, iDays As Integer
Dim dYearsAddedDate As Date, dMonthsAddedDate As Date
Dim sDifference As String

iYears = DateDiff("yyyy", pdStDate, pdEndDate)
dYearsAddedDate = DateAdd("yyyy", iYears, pdStDate)
If dYearsAddedDate > pdEndDate Then
  'dates were less than one year apart, so subtract 1
  iYears = iYears - 1
  dYearsAddedDate = DateAdd("yyyy", iYears, pdStDate)
End If

iMonths = DateDiff("m", dYearsAddedDate, pdEndDate)
dMonthsAddedDate = DateAdd("m", iMonths, dYearsAddedDate)
If dMonthsAddedDate > pdEndDate Then
  'dates were less than one month apart, so subtract 1
  iMonths = iMonths - 1
  dMonthsAddedDate = DateAdd("m", iMonths, dYearsAddedDate)
End If

iDays = DateDiff("d", dMonthsAddedDate, pdEndDate)
sDifference = iYears & IIf(iYears = 1, " year, ", " years, ") _
& iMonths & IIf(iMonths = 1, " month, ", " months, ") & iDays _
& IIf(iDays = 1, " day", " days")

YMD_DateDiff = sDifference

End Function

I suspect that it is in the field expression that I should make the ajustment but unsure about how best to achieve it?
0
Comment
Question by:MGardner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39235193
Try this for the calling expression

 = ymd_datediff( startdate, nz(enddate, date()))
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39235202
I would suggest to set the parameter date, if NULL, to todays date right at the beginning of the function as:
IF isNULL(pdEndDate) then
    pdEndDate=Date()
end if

Open in new window

0
 
LVL 2

Author Closing Comment

by:MGardner
ID: 39235251
excellent solution, many thanks
0
 
LVL 2

Author Comment

by:MGardner
ID: 39235261
I tried testing for a null date in the in the function exactly as above but got a Error# but thanks any ways much appreciated
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question