Hi, Matt.
The function definition should be entered in the form's Code Module. There
are a number of ways to enter the module, probably the easiest is, from Form
Design View, pressing <Ctrl-G>, which opens the module and places you in the
"Immediate Window"--used for testing the values of expressions as a debugging
tool. Move your cursor to the window above the Immediate Window, and enter
the function definition there.
Any function defined in a form's module has scope limited to that form,
which is fine here. If you dream up more general functions that you'd like
to access from anywhere, they should be defined in a general module (click
the Module tab, define a new module), and should be given universal scope
with the keyword Public:
Public Function MyGeneralFunction() As ...
....
End Function
As to your form control, once the function is defined in its form module,
you get its value by assigning the ControlSource to the value of the function:
=MyResult()
Hope that helps.
Sprinks
Matt said:
Thanks for your response Sprinks. Would I enter this function in the
Expression Builder? This looks like a Visual Basic function. I could be
wrong, I haven't used it in a while. If this is a Visual Basic function,
would I need to write this in another place in my database and then reference
it in my control source?
:
Hi, Matt.
I can’t see any typos, although verify that [Plan] is truly a text field
rather than merely displaying text yet storing an underlying code.
I think, though, that complex nested IF function calls are better replaced
by a custom function stored in the form module, being much easier to read and
debug. You can also combine the first three into a single boolean value with
the OR conjunction:
Function MyResult() As Single
If ([Plan] = “Smart Trunk†OR [Plan] = “Super Trunk†OR [Plan] = “IASâ€)
Then
MyResult = [NNI]*[Term Length]
Else
If [Completelink] = 0 Then
MyResult = [Trunks/Lines] * [RU] + _
[Trunks/Lines2] * [RU2] + _
[Trunks/Lines3] * [RU3] + _
[Trunks/Lines4] * [RU4]
MyResult = MyResult * (1-[DiscountRate]) * [Term Length]
Else
MyResult = [Completelink] * [Term Length] / 12
End If
End If
End Function
Then set the ControlSource to: = MyResult()
Hope that helps.
Sprinks
:
I am trying to write an If statement in my form that calculates the total
contract value of a sale. The text box is locked so people can't change
anything, but I want it to show them how much they sold to a customer. Here
is the formula that I'm trying to use:
=IF([Plan]="Smart Trunk",[NNI]*[Term Length],IF([Plan]="Super
Trunk",[NNI]*[Term Length],IF([Plan]="IAS",[NNI]*[Term
Length],IF([Completelink]=0,((([Trunks/Lines]*[RU])+([Trunks/Lines2]*[RU2])+([Trunks/Lines3]*[RU3])+([Trunks/Lines4]*[RU4]))*(1-[Discount
Rate]))*[Term Length],[Completelink]*([Term Length]/12)))))
I don't know if this makes sense, but can anyone let me know why this isn't
working?