Good additional info and suggested change to Control Source, Paul.
Thanks.
However, don't use Me in the ControlSource directly; instead, just use
the
checkbox control's name:
=IIf([isFree], 0, [quantity]*[unitprice])
--
Ken Snell
http://www.accessmvp.com/KDSnell/
Expanding on Ken's note, you need to call the same code anytime either
the
current record changes or the check box value changes, assuming the
checkbox is bound to a field in your data. I would put the code to
update
the textbox display in it's own routine and then call that routine both
from cbxDeal_AfterUpdate and in a FormCurrent event handler. And Ken is
also correct that the whole approach fails if you're using a continuous
form view.
If you don't have the check box bound to a data field, this won't work
because there's no way to retain the appropriate display once you leave
the current record.
Maybe a better approach is to change the txtPrice control source so the
control source accounts for the checkbox value. It would be something
like:
= iif(Me.isFree, 0, [quantity]*[unitprice])
assuming the checkbox control is bound to a data field named isFree
(which
should have a default value of false [0] and not allow nulls). Then the
text box always displays the correct value, without any additional
code,
and continous forms would work correctly.
You can use the AfterUpdate event of the checkbox to change the
ControlSource of the textbox:
Private Sub cbxDeal_AfterUpdate()
Select Case Me.cbxDeal.Value
Case True ' checkbox is checked
Me.txtPrice.ControlSource = ""
Me.txtPrice.Value = 0
Case False ' checkbox is unchecked
Me.txtPrice.ControlSource = "=[quantity]*[unitprice]"
End Select
End Sub
Note that this will affect all records on the form if your form shows
more than one record at a time (Continuous Forms view).
--
Ken Snell
http://www.accessmvp.com/KDSnell/
On my form I have a text box (txtPrice) which currently has a control
source
as =[quantity]*[unitprice] and a currency format.
I also have a check box (cbxDeal) which when checked I want it to
override
the value already in txtPrice and make it $0.00. Then if its
unchecked
the
calculated value (=[quantity]*[unitprice]) returns.
I have tried putting a code in the on click event something like
this:
If me.cbxdeal = true then
me.price.value = "0"
End if
Doesn't work
.