We help IT Professionals succeed at work.

Add days to date if checked

Cyprexx IT
Cyprexx IT asked
on
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...
Comment
Watch Question

Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
Commented:
Something like this:

Private Sub YourCheckBoxName_AfterUpdate()
     If Me.YourCheckBoxName = False Then Exit Sub
     Me.YourDateTextBox = DateAdd("d",60, Me.YourDateTextBox)
End Sub

mx
You may try this one too:

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

Ed
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:
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



       
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
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)
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
...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
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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

mx
Dale FyeOwner, Dev-Soln LLC
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2010

Commented:
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.
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
^
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.