Group:  Microsoft Access ยป microsoft.public.access.modulescoding
Thread: Set optional value in function, how to?

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Set optional value in function, how to?
"SF" <ssamnang[ at ]yahoo.com> 25.08.2006 01:02:02
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


Re: Set optional value in function, how to?
strive4peace <strive4peace2006[ at ]yahoo.com> 25.08.2006 02:21:46
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
>
>
Re: Set optional value in function, how to?
"SF" <ssamnang[ at ]yahoo.com> 25.08.2006 08:08:18
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


Re: Set optional value in function, how to?
John Nurick <j.mapSoN.nurick[ at ]dial.pipex.com> 25.08.2006 20:50:22
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.
Re: Set optional value in function, how to?
strive4peace <strive4peace2006[ at ]yahoo.com> 25.08.2006 22:47:28
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.

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net