Auto populate date field based on another date field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yappy77
    New Member
    • Apr 2010
    • 37

    Auto populate date field based on another date field

    I want my "requalify date" to auto populate to the beginning of the month in the following year when my "liability date" gets entered/changed.

    Example: Liablity date = 11/15/2010; 7/31/2011

    Requalify date should be 11/1/2011; 7/1/2012

    I am not sure how to write the formula to get this result.

    The requalify date is a field in my main table as well as the liability date. When entering the formula, would entering it in the requalify date field's default value be the best.

    Any help would be much appreciated. Thank you!

    Yappy
  • Steven Kogan
    Recognized Expert New Member
    • Jul 2010
    • 107

    #2
    Try using DateSerial(year , month, day) combined with DatePart(interv al, date) to build your formula. The formula for requalify date can be:

    DateSerial(Date Part("y",[Liability Date])+1,DatePart("m ",[Liability Date]),1))

    Comment

    • Steven Kogan
      Recognized Expert New Member
      • Jul 2010
      • 107

      #3
      Sorry, that should be:
      DateSerial(Date Part("yyyy",[Liability Date])+1,DatePart("m ",[Liability Date]),1)

      Comment

      • yappy77
        New Member
        • Apr 2010
        • 37

        #4
        Hi, Steven!
        Thanks for your reply. I do have another question since I am not sure if I am placing the expression in the correct area.

        When entering the formula, would entering it in the requalify date field's default value be the best?

        I have tried that and am getting errors.

        Comment

        • Steven Kogan
          Recognized Expert New Member
          • Jul 2010
          • 107

          #5
          It could go in a query as a calculated field, or as the control source on the form.

          Using it as a default value wouldn't work, as you've found out.

          Did you want the value to be editable, or always based on a calculation? If you want it to be initially calculated after the liability date is entered, and then to be able to edit it after, you'd need to use VBA.

          The code would be something like this:
          Code:
          Private Sub Liability_Date_AfterUpdate()
              If IsDate([Liability Date]) Then
                  [Requalify Date] = DateSerial(DatePart("yyyy", [Liability Date]) + 1, DatePart("m", [Liability Date]), 1)
              End If
          End Sub

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32653

            #6
            You will find it complicated to keep updating the value. Not impossible, but complicated.

            This is not something you should really even be considering (See Normalisation and Table structures). A normalised database would calculate the related value as and when needed, but never store a value that can always be calculated reliably.

            Comment

            • Steven Kogan
              Recognized Expert New Member
              • Jul 2010
              • 107

              #7
              I agree: if the value can always be calculated then it should not be a field in your table. If that's the case, add Requalify Date as a calculated field in a query. The exception would be if the database is for Access 2010, which supports calculated fields in tables.

              The field in the query would be something like:

              Code:
              Requalify Date: DateSerial(DatePart("yyyy", [Liability Date]) + 1, DatePart("m", [Liability Date]), 1)
              Last edited by NeoPa; Aug 4 '10, 03:57 PM. Reason: Please use the [CODE] tags provided

              Comment

              • yappy77
                New Member
                • Apr 2010
                • 37

                #8
                Thank you so much! That is exactly what I needed. I tested it and it works perfectly.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32653

                  #9
                  Steven,

                  you may want to check your PMs :)

                  Comment

                  • Steven Kogan
                    Recognized Expert New Member
                    • Jul 2010
                    • 107

                    #10
                    Hi NeoPa,

                    I'll be sure to include code in the [CODE] tags.

                    I'm unable to reply (regarding changing my status to Expert) with a PM. I haven't sent any, but I get the message:
                    Registered members under 50 posts can only send 3 private messages a day.

                    Comment

                    Working...