How To Calculate Date Intervals With VBA DateAdd

One of my clients ask me to design a little utility to operate like the Outlook appointment
recurrence feature.

This the first basic step, so I wanted to share how I did it.

I have a basic form where I’ll probably be adding more variables, but the functionality basically works like this:

I have an input box for the start date, and how many times the occurrence should happen, and then at what frequency (monthly, quarterly, yearly, or semiannually)

How_To_Calculate_Date_Intervals_With_VBA_DateAdd

Option Compare Database
'-----------------------------------
'CODE BY LOEBLCOMSERVICES 2018
'erik@loeblcomservices.com
'-----------------------------------


Private Sub Form_Load()

    'Initialize the text boxes
    
    Me.txtStartDate = Date
    Me.txtRecur = 1
    
End Sub


Private Sub btnAddNew_Click()
 
    Dim intFrequency As Integer
    
    'Get the first date
    dteStartDate = CDate(Me.txtStartDate)
    
    'How many date intervals will we need?  Default to 1, if this is blank.
    intFrequency = Nz(Me.txtRecur, 1)
    
    'Delete what's currently in the table
    CurrentDb.Execute "DELETE * FROM tblFrequencies"
    
    'Select the date frequency based on an option group, and pass the _
        required information to the next procedure.
    
    Select Case Me.fraFrequency
        Case 1 'monthly
            Frequencies dteStartDate, "m", 1, intFrequency
        Case 2 'quarterly
            Frequencies dteStartDate, "m", 3, intFrequency
        Case 3 'yearly
            Frequencies dteStartDate, "m", 12, intFrequency
        Case 4 'semi annually
            Frequencies dteStartDate, "m", 6, intFrequency

    End Select
    
    DoCmd.OpenQuery "qryFrequencies"

End Sub

Sub Frequencies(dteStartDate, interval, subinterval, times)
    
    'Insert the start date into the table
    CurrentDb.Execute "INSERT INTO tblFrequencies (DateNum,NewDate) VALUES(1,'" & dteStartDate & "')"
    
    'Get the next date due by using the DateAdd function
    dteNextDueDate = DateAdd(interval, subinterval, dteStartDate)
    Debug.Print "Next Date Due= " & dteNextDueDate
    
    'Insert the next date due.
    CurrentDb.Execute "INSERT INTO tblFrequencies (DateNum,NewDate) VALUES(2,'" & dteNextDueDate & "')"
         
    'Alternate between the next 2 procedures until the "times" variable is reached
    Getdates1 dteNextDueDate, interval, subinterval, 2, times
    
End Sub

Sub Getdates1(NextDueDate, interval, intSubInterval, current, TotalTimes)
    
    If current < TotalTimes Then
        
        'Use the DateAdd function again to get the next iteration date
        dteNextDueDate = DateAdd(interval, intSubInterval, NextDueDate)
        
        Debug.Print dteNextDueDate
        
        'Increment the counter to pass to the next procedure
        current = current + 1
        
        'Insert the value
        CurrentDb.Execute "INSERT INTO tblFrequencies (DateNum,NewDate) VALUES(" & current & ",'" & dteNextDueDate & "')"
               
        Getdates2 dteNextDueDate, interval, intSubInterval, current, TotalTimes
    End If
End Sub

Sub Getdates2(NextDueDate, interval, intSubInterval, current, TotalCt)
        
    'Use the DateAdd function again to get the next due date
    dteNextDueDate = DateAdd(interval, intSubInterval, NextDueDate)
    
    
    Debug.Print dteNextDueDate
    
    'Increment the counter to pass to the next procedure
    current = current + 1
    
    'Insert the value
    CurrentDb.Execute "INSERT INTO tblFrequencies (DateNum,NewDate) VALUES(" & current & ",'" & dteNextDueDate & "')"

  
    'Call the first procedure again until the interval is reached
    Getdates1 dteNextDueDate, interval, intSubInterval, current, TotalCt
    

End Sub


Click here to download the sample database: “How To Calculate Date Intervals With VBA DateAdd”




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Create A Parameter Query In Access

A parameter query changes your ordinary static access query to be more dynamic and interactive. It will ask you a question about what you want to search for, allowing you to do a search query multiple times instead of just once. You can do your parameter query straight from the QBE (Query By Example) Editor, […]

Previous Post

MS Access VBA Basics – Video 1

Next Post

How To Make A VBA Message Box OK Cancel