Calculating Summer Days in Microsoft Access: A Step-by-Step Guide

Counting Days in a Product Life Excluding the Summers

As a product manager or analyst working with a dataset of product life cycles, you may need to calculate the number of days that have passed since a date in two-year periods: “summer” and non-summer. In this article, we’ll explore how to achieve this using Microsoft Access SQL queries.

Problem Statement

The question is as follows:

“I am trying to write an ms access SQL query in order to calculate the number of days that have passed since a date in two year periods: ‘summer’ (from 1/6 to 30/09) and non-summer (from 1/1 to 30/05 and from 1/10 to 31/12).

Then I would like to know, as today, how many days of summer and vice-versa how many days of nonsummer has passed since the product has been measured.”

Background

In this article, we’ll delve into the world of VBA functions in Microsoft Access. We’ll explore a simple yet effective way to calculate summer and non-summer days using a defined function.

Calculating Summer Days

The first step is to define a VBA function that calculates the number of summer days between two dates. Here’s an example of how this function can be implemented:

Defining the Function

Function SummerDays(ByVal dateFrom As Date, ByVal dateTo As Date) As Long
    While dateFrom < dateTo
        If dateFrom >= DateSerial(Year(dateFrom), 6, 1) And dateFrom <= DateSerial(Year(dateFrom), 9, 30) Then
            SummerDays = SummerDays + 1
        End If
        dateFrom = dateFrom + 1
    Wend
End Function

This function takes two parameters: dateFrom and dateTo, representing the start and end dates of the period. It uses a While loop to iterate through each day in the range, checking if the current date falls within the summer period (i.e., between June 1st and September 30th). If it does, the function increments the SummerDays counter.

Calling the Function

To calculate the number of summer days for a given date, you can call this function from your query. Here’s an example:

SummerDays(G2.DATA_PRODUZ, Date())

Calculating Non-Summer Days

Now that we have a function to calculate summer days, let’s explore how to calculate non-summer days.

One way to do this is to define another VBA function with an inverse If statement. Here’s an example:

Defining the Non-Summer Function

Function NonSummerDays(ByVal dateFrom As Date, ByVal dateTo As Date) As Long
    SummerDaysCount = SummerDays(dateFrom, dateTo)
    NonSummerDaysCount = DateDiff("d", G2.DATA_PRODUZ, Date()) - SummerDaysCount
End Function

This function calculates the number of non-summer days by calling the SummerDays function to get the count of summer days and then subtracting this from the total number of days between the two dates.

Creating a Public Module

To define these functions, you’ll need to create a public module in Microsoft Access. Here’s how:

Creating a New Public Module

  1. Open the VBA IDE using Alt + F11.
  2. Insert a new public module by going to Insert > Module or pressing Alt, I, M.
  3. Copy the above function definitions into the Module, below the Option statements.
  4. Save the Module with an appropriate name.

Example Use Case

Here’s an example of how you can use these functions in a Microsoft Access query:

SELECT 
    G2.DATA_PRODUZ AS DATE,
    SummerDays(G2.DATA_PRODUZ, Date()) AS SUMMER_DAYS,
    NonSummerDays(G2.DATA_PRODUZ, Date()) AS NON_SUMMER_DAYS
FROM 
    YourTable;

This query will return the date, summer days, and non-summer days for each record in your table.

Conclusion

In this article, we explored how to calculate the number of summer and non-summer days using a defined VBA function in Microsoft Access. We covered the basics of defining a public module, creating functions, and calling these functions from queries. With these techniques, you can easily calculate summer and non-summer days for any given date range.

Additional Tips

  • Make sure to save your Module with an appropriate name to avoid confusion when referencing it in your queries.
  • Use meaningful function names and comments to make your code easier to understand and maintain.
  • Consider using a more robust date calculation method, such as using DateAdd or DateDiff, to ensure accurate results.

By following these steps and techniques, you can easily calculate summer and non-summer days for any given date range. Happy coding!


Last modified on 2025-04-30