Search code examples
date-rangems-access-2016criteriaquery

MS Access - Multiple Queries shared Criteria in time stamp date range


MS Access 2016,

I have Multiple queries - approximately 120 - that are gathering temp history based on date criteria that I currently update manually for each query - >=#8/1/2021# And <#9/1/2021# - for the month of August 2021.

What would be the best solution to update this in one place so all queries could refer to that one date range?

Sample Query: (Usually 43 parameter points)

SELECT 
    History1HourAverage.TimeStamp, 
    History1HourAverage.Parameter001, 
    History1HourAverage.Value001, 
    History1HourAverage.Parameter002, 
    History1HourAverage.Value002, 
    History1HourAverage.Parameter003, 
    History1HourAverage.Value003, 
    History1HourAverage.Parameter004, 
    History1HourAverage.Value004
FROM History1HourAverage
WHERE 
    (
        (
            ***(History1HourAverage.TimeStamp)>=#8/1/2021#
            AND (History1HourAverage.TimeStamp)<#9/1/2021#
        )*** 
        AND ((History1HourAverage.Parameter001)="10S8MApp.nvoSpaceTemp") 
        AND ((History1HourAverage.Parameter002)="10S9MApp.nvoSpaceTemp") 
        AND ((History1HourAverage.Parameter003)="10S10MApp.nvoSpaceTemp") 
        AND ((History1HourAverage.Parameter004)="10S11MApp.nvoSpaceTemp")
    );

Thank you


Solution

  • A couple of options:

    Either

    Add a table called DateRange with two fields, StartDate and EndDate. Enter one record in that table with the date range that you want to use for your queries. Edit each of your queries and include the DateRange table. Set the criteria for the query to WHERE History1HourAverage.TimeStamp >= DateRange.StartDate And History1HourAverage.TimeStamp < DateRange.EndDate

    Alternatively

    Create a VBA module with two functions

    Public Function StartDate() As Date
        StartDate = #8/1/2021#
    End Function
    
    Public Function EndDate() As Date
        EndDate = #9/1/2021#
    End Function
    

    Set your query criteria for the query to WHERE History1HourAverage.TimeStamp >= StartDate() And History1HourAverage.TimeStamp < EndDate()

    When you want to use a different date range you either (1) edit the data in DateRange table, or (2) edit your functions to return the new dates.