Link to home
Start Free TrialLog in
Avatar of Cyprexx IT
Cyprexx IT

asked on

Add days to date if checked

I have a check box that needs to add 60's straight days to a date when checked.

I'm not sure on the formla, if anyone could provide this for me...
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may try this one too:

Private Sub chkBox_Click()
Me.Date = Me.Date + 60
End Sub

Ed
Where are you adding this?  Are you displaying it in a textbox, or something like that?

If so, you could use the control source property, but if you don't want to display that value when the checkbox is not checked, then you are going to need to use the Click event of the checkbox, something like:

Private Sub chk_Add60_Click

    me.lbl_Add60.visible = me.chk_Add60
    me.txt_Add60.visible = me.chk_Add60
    if me.chk_Add60 = true then
        me.txt_Add60 = dateadd("d", 60, dtYourDate)
    else
        me.txt_Add60 = NULL
    End IF

End Sub

Then, in the Current event of your form, you will need to call this subroutine, so that the label and textbox will display properly

Private Sub Form_Current

    Call chk_Add60_Click

End Sub



       
The issue with doing this with a checkbox and forcing a field to update is the scenario where a use might check the box a few times out of confusion, or by mistake...

What is your plan for the user checking the box On, Off, then On again...?
Add 120 days...?
Add 60, subtract 60, add sixty?
Only add 60 days...? (then you have to have some type of logic to limit the "Checks", by session, by record, per update, ...etc)
...This is why it is always useful to post the reason for needing something.

This way we know all the facts and can suggest options with a bit more certainty...

;-)

Jeff
MINDSUPERB:
Unfortunately, the 60 days will be added if the check box is False.

mx
BTW,

I would generally store the status of the checkbox in your table, and then just display the computed date in an unbound textbox.

However, you could include something like the following in the RecordSource of your form:

SELECT [YourDate], [Add60], IIF([Add60], Dateadd("d", 60, [YourDate]), NULL) as ComputedDate
FROM yourTable

If you did this, then anytime your Add60 checkbox gets checked or unchecked, the ComputedDate field would get recomputed.
^
Yeah that was what I was concerned about...

The reason *Why* was this needed, and was any Validation/Protection/RollBack logic required...
mx,

Yeap. You are right. Thanks for the feedback.

Ed