Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17

    Calculating Service Period from Statement Date

    Hello All,



    I'm use a form as a data entry that flows to a table. In the data entry form I have a field titled "StatementDate", that field is used to calculate the "DueDate". I'm trying to use the StatementDate field to calculate a ServicePeriod field as well with the following criteria:

    If the statement day is the 15th or later than the service period would be the next month, if before the 15th the service period is the month of the StatementDate. Example

    StatementDate = 1/14/2013, then the ServicePeriod would be 01/2013
    StatementDate = 1/15/2013, then the ServicePeriod would be 02/2013


    The VBA Code to calculate the DueDate is as follows:

    Private Sub DueDate_Enter()
    DueDate = DateAdd("d", 14, Me.StatementDate)
    End Sub

    I'm not sure, but I'm guessing I'll have to use the DatePart function to pull the day from the StatementDate?

    Any input or tips on trying to achieve the desired results?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Have you looked at the Day() function yet?

  3. #3
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    I hadn't, but that would be easier than the DatePart function. I will admit, I'm not the best at VBA coding when it comes to If/Then/Elseif statements.

    So from my example above of StatementDate = 1/14/2013, I'd want the ServicePeriod to be 1/1/13 and then the format of the field in the table converts it to 01/2013, and if StatementDate = 1/15/2013 I'd want the ServicePeriod to be 2/1/2013.

    Having the consistent Service period of the 1st day of the month makes it easier to filer, at least how I have it set up right now. Is there an easier way to do that, or is that the best way?

  4. #4
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    Finally got a chance to get back to building my database today, I'm not sure if the following code would work... I'm going to say no, but maybe this blind squirrel found a nut?

    Private Sub ServicePeriod_Enter()


    ServicePeriod = If Day([StatementDate]) >= 15 Then [ServicePeriod] = "(Month([StatementDate]+1),1,(year([StatementDate]))
    Elseif Day([StatementDate]) < 15 [ServicePeriod] = "(Month([StatementDate]),1,(year([StatementDate]))
    End If

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You've got the right idea but you don't need the test If < 15. What else could it be? You will also work out your construct when you compile it.

  6. #6
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    I'm getting a lot closer...

    Code:
    Private Sub ServicePeriod_Click()
     If Day([StatementDate]) >= 15 Then
    [ServicePeriod] = "(Month([StatementDate]+1),1,(year([StatementDate]))"
    Else: [ServicePeriod] = "(Month([StatementDate]),1,(year([StatementDate]))"
    End If
    End Sub
    But now I'm getting an error:
    "Run-time error '-2147352567 (80020009)'"
    The Value you entered isn't valid for this field.

    So do I need something about Date Format in the code?

    I tried putting # in front of and behind the result sequence, but that returned the same error.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try:
    Code:
    Private Sub ServicePeriod_Click()
        If Day([StatementDate]) > 14 Then
           [ServicePeriod] = DateSerial(Year([StatementDate),Month(StatementDate)+1),1)
       Else
           [ServicePeriod] = DateSerial(Year([StatementDate),Month(StatementDate),1)
       End If
    End Sub

  8. #8
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    Fan-freakin-tastic!!!

    Thank you very much for the help!

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You're very welcome. Keep in mind the code does not work in December.

  10. #10
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    Looks like I may have spoken too soon, it isn't doing the month +1 if the StatementDate is over 14.

    Code:
    Private Sub StatementDate_AfterUpdate()
        If Day([StatementDate]) > 14 Then
        [ServicePeriod] = DateSerial(Year([StatementDate]), Month([StatementDate]), 1)
       Else
        [ServicePeriod] = DateSerial(Year([StatementDate]), (Month([StatementDate]) + 1), 1)
       End If

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Can you single step the code and see what the Day() is returning?

  12. #12
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    I put in 1/14/13 as the StatementDate, it's returning 01/1900 as the result. If I click on the cell ServicePeriod in the form it shows 1/13/1900. That's using this code:
    Code:
    Private Sub StatementDate_AfterUpdate()
      ServicePeriod = Day([StatementDate])

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is your [StatementDate] a field in a table or a calculated field in a query?

  14. #14
    tkbeard29 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Posts
    17
    It's a field in a table, as is ServicePeriod.

    StatementDate - Date/Time (format mm/dd/yy)
    ServicePeriod - Date/Time (format mm/yyyy)

  15. #15
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Are you displaying the [StatementDate] and is the control also named [StatementDate]? If so then change the Control name to [txtStatementDate] and see if the results change. Do you know how to single step code?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Alert on Existing Date Period
    By waqas in forum Access
    Replies: 5
    Last Post: 02-03-2013, 10:38 PM
  2. Replies: 3
    Last Post: 12-04-2012, 05:22 PM
  3. Calculating a date range
    By unicorn in forum Queries
    Replies: 6
    Last Post: 01-18-2012, 08:58 AM
  4. Calculating Expiry Date -
    By Jojojo in forum Programming
    Replies: 12
    Last Post: 10-05-2011, 12:05 PM
  5. Getting Tax Period from current date
    By crxftw in forum Forms
    Replies: 3
    Last Post: 07-09-2011, 07:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums