|
|
Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
I have function below. I want to set the default value of the second date paramerter to take the current date if second date is not supplied. It doen't work
Public Function GetElapeDateInDay(StartDate As Date, Optional EndDate As Date = Date) As Long GetElapeDateInDay = DateDiff("d", StartDate, EndDate) End Function
SF
|
|
here are 2 different ways:
Public Function GetElapeDateInDay( _ StartDate As Date, _ Optional EndDate As Date) _ As Long
dim mEndDate as date
if ismissing(EndDate) then mEndDate = date() else mEndDate = EndDate end if
GetElapeDateInDay = _ DateDiff("d", StartDate, mEndDate ) End Function
'~~~~~~~~~~`
and another way...
Public Function GetElapeDateInDay( _ StartDate As Date, _ Optional EndDate As Date) _ As Long
dim mEndDate as date
if nz(EndDate,0) <> 0 then mEndDate = EndDate else mEndDate = date() end if
GetElapeDateInDay = _ DateDiff("d", StartDate, mEndDate )
End Function
Ideally, you would put some error checking in to make sure StartDate has a value...
Warm Regards, Crystal * (: have an awesome day :) * MVP Access Remote Programming and Training strive4peace2006 at yahoo.com *
SF wrote:
[Quoted Text] > I have function below. I want to set the default value of the second date > paramerter to take the current date if second date is not supplied. It > doen't work > > > Public Function GetElapeDateInDay(StartDate As Date, Optional EndDate As > Date = Date) As Long > GetElapeDateInDay = DateDiff("d", StartDate, EndDate) > End Function > > SF > >
|
|
Thank you!. Both are working SF
"strive4peace" <strive4peace2006[ at ]yahoo.com> wrote in message news:OYbB60%23xGHA.4524[ at ]TK2MSFTNGP04.phx.gbl...
[Quoted Text] > here are 2 different ways: > > Public Function GetElapeDateInDay( _ > StartDate As Date, _ > Optional EndDate As Date) _ > As Long > > dim mEndDate as date > > if ismissing(EndDate) then > mEndDate = date() > else > mEndDate = EndDate > end if > > GetElapeDateInDay = _ > DateDiff("d", StartDate, mEndDate ) > End Function > > '~~~~~~~~~~` > > and another way... > > Public Function GetElapeDateInDay( _ > StartDate As Date, _ > Optional EndDate As Date) _ > As Long > > dim mEndDate as date > > if nz(EndDate,0) <> 0 then > mEndDate = EndDate > else > mEndDate = date() > end if > > GetElapeDateInDay = _ > DateDiff("d", StartDate, mEndDate ) > > End Function > > Ideally, you would put some error checking in to make sure StartDate has a > value... > > Warm Regards, > Crystal > * > (: have an awesome day :) > * > MVP Access > Remote Programming and Training > strive4peace2006 at yahoo.com > * > > > > SF wrote: >> I have function below. I want to set the default value of the second date >> paramerter to take the current date if second date is not supplied. It >> doen't work >> >> >> Public Function GetElapeDateInDay(StartDate As Date, Optional EndDate As >> Date = Date) As Long >> GetElapeDateInDay = DateDiff("d", StartDate, EndDate) >> End Function >> >> SF
|
|
Hi Crystal,
This one doesn't do what you expect: IsMissing() only works with a Variant argument. With simple types such as Date it always returns False. An alternative approach is to use a magic number for the default value, e.g.
Public Function GetElapeDateInDay( _ StartDate As Date, _ Optional ByVal EndDate As Date = #12/31/1899#) _ As Long
If EndDate = #12/31/1899# Then EndDate = Date() End If
GetElapeDateInDay = _ DateDiff("d", StartDate, EndDate) End Function
On Thu, 24 Aug 2006 22:21:46 -0400, strive4peace <strive4peace2006[ at ]yahoo.com> wrote:
[Quoted Text] >Public Function GetElapeDateInDay( _ > StartDate As Date, _ > Optional EndDate As Date) _ > As Long > > dim mEndDate as date > > if ismissing(EndDate) then > mEndDate = date() > else > mEndDate = EndDate > end if > > GetElapeDateInDay = _ > DateDiff("d", StartDate, mEndDate ) >End Function
-- John Nurick [Microsoft Access MVP]
Please respond in the newgroup and not by email.
|
|
Thank you, John!!!
I could never figure out why I had to start testing for empty string,,, just did. IsMissing used to work (before I got to be a better programmer and started declaring data types...now I know why!)
I like putting a default value in like that, thanks again John
Warm Regards, Crystal * (: have an awesome day :) * MVP Access Remote Programming and Training strive4peace2006 at yahoo.com *
John Nurick wrote:
[Quoted Text] > Hi Crystal, > > This one doesn't do what you expect: IsMissing() only works with a > Variant argument. With simple types such as Date it always returns > False. An alternative approach is to use a magic number for the default > value, e.g. > > Public Function GetElapeDateInDay( _ > StartDate As Date, _ > Optional ByVal EndDate As Date = #12/31/1899#) _ > As Long > > If EndDate = #12/31/1899# Then > EndDate = Date() > End If > > GetElapeDateInDay = _ > DateDiff("d", StartDate, EndDate) > End Function > > > On Thu, 24 Aug 2006 22:21:46 -0400, strive4peace > <strive4peace2006[ at ]yahoo.com> wrote: > >> Public Function GetElapeDateInDay( _ >> StartDate As Date, _ >> Optional EndDate As Date) _ >> As Long >> >> dim mEndDate as date >> >> if ismissing(EndDate) then >> mEndDate = date() >> else >> mEndDate = EndDate >> end if >> >> GetElapeDateInDay = _ >> DateDiff("d", StartDate, mEndDate ) >> End Function > > -- > John Nurick [Microsoft Access MVP] > > Please respond in the newgroup and not by email.
|
|
|