Function question

G

Guest

I have a function to calculate a contract's ending date. Instead of basing
it on the contract's start date, I need to compare 2 dates (acceptance_date
or contract_start_date) and use only the greater of the 2 dates. Can you use
an "if" statement within a function? I am doing something terribly wrong.

Here is my function:
Public Function CalcEndDate (optionframe as integer, term as integer, start
as date)
Select Case optionframe
Case 1
CalcEndDate = DateAdd("m",term,start)-1
Case 2
CalcEndDate = DateAdd("ww",term,start)
End Select
End Function

Private Sub Acceptance_date_Change( )

If Not IsNull(optChoose) and Not IsNull(Term_mos) and Not
IsNull(acceptance_date) Then
If Acceptance_date < Contract_start_date
Acceptance_date = contract_start_date
Else
Acceptance_date = Acceptance_date
End If
Me!contract_end_date = CalcEndDate)[optChoose],[term_mos],[Acceptance_date]
End Sub

Thank you in advance for your help!
 
G

Guest

gg

Indeed you can have if statements within functions. Without knowing exactly
what problem you are getting it is difficult to advise.

Looking at your call though there would appear to be a typo (backwards
bracket) and no end bracket. Also if optChoose, term_mos and Acceptance_date
are fields on your form, they need to be prefixed me.optchoose etc.

If you can be more specific as to what the problem is I may be able to help
 
G

Guest

Hi Chris,

I have 3 dates on my main form: contract_start_date, acceptance_date and the
calculated contract_end_date. I wish to calculate the contract's end date
based on 1) the greater of the 2 dates-acceptance_date vs.
contract_start_date 2) term-number of months. I have a public function that
calculated the end date based on the acceptance date and term however, now I
wish to calculate the end date based on the greater of the 2 starting dates.
How do I go about doing that? I know I need some "if" logic somewhere. :)

Thank you!



Chris said:
gg

Indeed you can have if statements within functions. Without knowing exactly
what problem you are getting it is difficult to advise.

Looking at your call though there would appear to be a typo (backwards
bracket) and no end bracket. Also if optChoose, term_mos and Acceptance_date
are fields on your form, they need to be prefixed me.optchoose etc.

If you can be more specific as to what the problem is I may be able to help
--
HTH

Chris


gg said:
I have a function to calculate a contract's ending date. Instead of basing
it on the contract's start date, I need to compare 2 dates (acceptance_date
or contract_start_date) and use only the greater of the 2 dates. Can you use
an "if" statement within a function? I am doing something terribly wrong.

Here is my function:
Public Function CalcEndDate (optionframe as integer, term as integer, start
as date)
Select Case optionframe
Case 1
CalcEndDate = DateAdd("m",term,start)-1
Case 2
CalcEndDate = DateAdd("ww",term,start)
End Select
End Function

Private Sub Acceptance_date_Change( )

If Not IsNull(optChoose) and Not IsNull(Term_mos) and Not
IsNull(acceptance_date) Then
If Acceptance_date < Contract_start_date
Acceptance_date = contract_start_date
Else
Acceptance_date = Acceptance_date
End If
Me!contract_end_date = CalcEndDate([optChoose],[term_mos],[Acceptance_date]
End Sub

Thank you in advance for your help!
 
J

John Vinson

Hi Chris,

I have 3 dates on my main form: contract_start_date, acceptance_date and the
calculated contract_end_date. I wish to calculate the contract's end date
based on 1) the greater of the 2 dates-acceptance_date vs.
contract_start_date 2) term-number of months. I have a public function that
calculated the end date based on the acceptance date and term however, now I
wish to calculate the end date based on the greater of the 2 starting dates.
How do I go about doing that? I know I need some "if" logic somewhere. :)

How about no code at all?

Set the Control Source for contract_end_date to

=DateAdd("m", [Term], IIF([Contract_Start_Date] > [Acceptance_Date],
[Contract_Start_Date], [Acceptance_Date]))


John W. Vinson[MVP]
 
G

Guest

Thanks, John! Simple and to the point! You're awesome.

John Vinson said:
Hi Chris,

I have 3 dates on my main form: contract_start_date, acceptance_date and the
calculated contract_end_date. I wish to calculate the contract's end date
based on 1) the greater of the 2 dates-acceptance_date vs.
contract_start_date 2) term-number of months. I have a public function that
calculated the end date based on the acceptance date and term however, now I
wish to calculate the end date based on the greater of the 2 starting dates.
How do I go about doing that? I know I need some "if" logic somewhere. :)

How about no code at all?

Set the Control Source for contract_end_date to

=DateAdd("m", [Term], IIF([Contract_Start_Date] > [Acceptance_Date],
[Contract_Start_Date], [Acceptance_Date]))


John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top