1

I am trying to implement the DateAdd function into my Microsoft Access 2010 form. I have a form that when the user inputs the date (property name called "txtStartDate", then I want the output in a different part to be 18 calendar days in the future (property name called "textEndDate").

I know the format will be

=DateAdd("d", 18, txtStartDate) 

but how do I write it in VBA code to implement?

2
  • I believe you have your criteria in the wrong order see: support.office.com/en-us/article/… so it should be =DateAdd("d",18, txtStartDate) Commented Sep 20, 2016 at 22:02
  • Thanks @ScottCraner Commented Sep 20, 2016 at 22:07

5 Answers 5

1

It sounds like you wish to set it after an update. Thus, use the AfterUpdate event of the start date:

Private Sub txtStartDate_AfterUpdate()

    Dim EndDate As Variant

    If IsDate(Me!txtStartDate.Value) Then
       EndDate = DateAdd("d", 18, Me!txtStartDate.Value)
    Else
       EndDate = Null
    End If
    Me!txtEndDate.Value = EndDate

End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this helps somewhat, two declarations for the original date and the new date. Msgbox then shows them both here on different lines:

sub addingDate()
Dim txtStartDate As Date
Dim txtEndDate As Date
    txtStartDate = Now 'Or wherever you get this info from
    txtEndDate = DateAdd("d", 18, txtStartDate)
    MsgBox (txtStartDate & vbNewLine & txtEndDate)
End Sub

Comments

0

It sounds like you're implicitly converting a textbox value from a String to a Date. You might want to add some checks like:

If IsDate(txtStartDate) Then

And you'll want to be particularly conscious of the behavior should your code be running on systems that have varying Regional Date settings for American/English/Japanese dates. That might require using a date-picker or some other date-validation on the control itself.

Comments

0

You don't need VBA to do that, only an expression as ControlSource for that other textbox:

=DateAdd("d", 18, [txtStartDate])

Comments

0

Try This:

= DateAdd("d", 18, cdate(txtStartDate)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.