×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

specifying default for optional argument
2

specifying default for optional argument

specifying default for optional argument

(OP)
If I make an argument optional, how do I specify the default for that argument?

Example:

Function PatientAge(Birthdate As Date, Optional EndDate As Date) As Integer
' calc patient age in months, if > 23 months old, recalc in years
' also use today's date if EndDate not specified

If AgeInMonths(Birthdate, EndDate) > 23 Then
    PatientAge = AgeinYears(Birthdate, Date)
Else
    PatientAge = AgeInMonths(Birthdate, EndDate)
End If

End Function

RE: specifying default for optional argument

What about
 If IsNull(EndDate) then EndDate = Date()

RE: specifying default for optional argument

I think you can use IsMissing() function,

 IF IsMissing(EndDate) Then
   "Put what you want to do here"

 End If

Mohamed Aly
samara_79@hotmail.com

RE: specifying default for optional argument

If you are wanting to specify the default in the declaration statement use this as an example:

Function PatientAge(Birthdate As Date, Optional EndDate As Date = Date()) As Integer

This example will insert the current date as the argument if the arugument is not specified.

HTH
RDH

Ricky Hicks
rdhicks@mindspring.com

RE: specifying default for optional argument

(OP)
Ricky, when I try to type:
Function PatientAge(Birthdate As Date, Optional EndDate As Date = Date()) As Integer
the VB Editor "fixes" it to become
Function PatientAge(Birthdate As Date, Optional EndDate As Date = Date) As Integer
Then when I try to compile this function I get:
Compile Error: Constant expression required

Lightning & Samara,
When I try your solutions, my answer is off by -100 years. It works if I change the birthdate to 100 years earlier, but I wonder if this has to do with some year2000 related thing...

They still work OK, BTW if I include date() as the EndDate when I call the function. I'm just trying to get fancy!

RE: specifying default for optional argument

The IsMissing function is designed for parameter of type variant, and returns true if a varient parameter is missing.  Unfortunately, givent the type structure it is syntactically incorrect to pass it a parameter of some other type, even though these parameter types are never missing.  They will always default to something of the correct type even if omitted from the call.

Declare your parameter as Variant instead of Data and you code should work correctly.

Of course, you may wantto be a little smarter than the implementor of IsMissing and check the parameter type at run time using the Vartype function.

RE: specifying default for optional argument

Guys,
i returned to my books and found out that any optional argument has to be of type Variant...

is that RIGHT ???

Mohamed Aly
samara_79@hotmail.com

RE: specifying default for optional argument

(OP)
Thank you! FYI, here's my now-working functions:

Function AgeInMonths(Birthdate As Date, Optional EndDate As Variant) As Integer
Dim NewEndDate As Date

If IsMissing(EndDate) Then
    NewEndDate = Date
Else
    NewEndDate = CDate(EndDate)
End If
AgeInMonths = DateDiff("m", Birthdate, NewEndDate) + _
(NewEndDate < DateSerial(Year(NewEndDate), Month(Birthdate), Day(Birthdate)))
End Function

Function AgeinYears(Birthdate As Date, Optional EndDate As Variant) As Integer
Dim NewEndDate As Date

If IsMissing(EndDate) Then
    NewEndDate = Date
Else
    NewEndDate = CDate(EndDate)
End If
AgeinYears = DateDiff("yyyy", Birthdate, NewEndDate) + _
(NewEndDate < DateSerial(Year(NewEndDate), Month(Birthdate), Day(Birthdate)))
End Function

Public Function PatientAge(Birthdate As Date, Optional EndDate As Variant) As String
' calc patient age in months, if > 24 months old, recalc in years
Dim NewEndDate As Date

If AgeInMonths(Birthdate) > 23 Then
    PatientAge = AgeinYears(Birthdate) & " years"
Else
    PatientAge = AgeInMonths(Birthdate) & " months"
End If

End Function

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close