Results 1 to 3 of 3
  1. #1
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62

    VBA function to return date of first direct deposit date in particular month

    I'm working on automating some processes for a company using MS Access to control Excel. One of the reports depends on when direct deposits happen in a month. A month can have two or three two-week pay periods depending on when the direct deposit dates fall. For example, in 12/22 there were two direct deposit dates of 12/14 and 12/28. (The pay period ending dates are always exactly seven days prior to the direct deposit date.) In 11/22, there were three direct deposit days of 11/2, 11/16 and 11/30. This made one of the November pay period ending date fall in October since it was a week before 11/2.



    What I'm trying to do is create a function that will return the first direct deposit date in any particular month of any year. I know the last direct deposit date of 2022 was 12/28 so I think the function will need to continue to add 14 days to 12/28/22 until it finds the sought after month in the sought after year. This is what I have so far:

    Code:
    Private Function FindFirstDDinMonth() As Date
        Dim i As Integer
        Dim Target Month As Integer
        Const IncrementDays As Integer = 14
        Target Month = Month(DateAdd("m", -1, ReportCreationDate))
        For i = 1 To 12
            If Month(DateAdd("d", i * IncrementDays, #12/28/2022#)) = Target Month Then
                FindFirstDDinMonth = DateAdd("d", i * IncrementDays, #12/28/2022#)
                Exit For
            End If
        Next i
    End Function
    This won't work, however, forever. I think the function needs to be recursive but I'm not sure.

    Thanks very much for your help.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    i use a table with the dates in it for the year.
    This avoids any calculations that could be wrong due to various events.

  3. #3
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    The answer is to use a Do...Until loop:

    Code:
    Function FindFirstDDinMonth(m As Integer, y As Integer) As Date
        Dim i As Integer
        Const IncrementDays As Integer = 14
        i = 1
        Do Until Month(DateAdd("d", i * IncrementDays, DirectDepositDate)) = m And Year(DateAdd("d", i * IncrementDays, DirectDepositDate)) = y
            i = i + 1
        Loop
        FindFirstDDinMonth = DateAdd("d", i * IncrementDays, DirectDepositDate)
    End Function
    Thanks for responding.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Get a Next Deposit Date depending on criteria?
    By d9pierce1 in forum Queries
    Replies: 19
    Last Post: 02-17-2023, 01:54 PM
  2. Replies: 1
    Last Post: 11-27-2017, 02:02 PM
  3. Replies: 2
    Last Post: 03-07-2013, 03:14 PM
  4. date function return value issue
    By live2ride in forum Access
    Replies: 3
    Last Post: 10-24-2012, 07:06 PM
  5. Replies: 3
    Last Post: 04-01-2012, 01:40 PM

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