Home > Mobile >  Access Previous Month Query
Access Previous Month Query

Time:10-19

Creating a query to pull data from a previous month only from a table. No luck with any of my research but just trying to pull data based off of a month column only. The table includes the following fields: Month, Type, Done and In Progress

Month field is listed January, March, etc.

Only trying to add a criteria in design view field. Any help would be appreciated. Thank you.

CodePudding user response:

It could be:

Select 
    *
From 
    YourTable
Where 
    MonthFromInvariant([Month]) = (Month(Date())   12 - 2) Mod 12   1;

using the functions:

' Returns the month number from the English month name.
' Abbreviated names are accepted.
' An ambigous abbreviation (i.e. "ju") will return the first match.
' Passing a non existing name or abbreviation will raise an error.
'
' For parsing localised month names, use function MonthValue.
'
' 2021-04-02. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function MonthFromInvariant( _
    ByVal MonthName As String) _
    As Integer

    Const FirstMonth    As Integer = MinMonthValue
    Const LastMonth     As Integer = MaxMonthValue
    
    Dim Month           As Integer
    
    MonthName = Trim(MonthName)
    If MonthName <> "" Then
        For Month = FirstMonth To LastMonth
            If InStr(1, MonthNameInvariant(Month, True), MonthName, vbTextCompare) = 1 Then
                Exit For
            End If
        Next
    End If
    If Month > LastMonth Then
        ' Month could not be found.
        Err.Raise DtError.dtTypeMismatch
        Exit Function
    End If
    
    MonthFromInvariant = Month

End Function



' Returns the English month name for the passed month number.
' Accepted numbers are 1 to 12. Other values will raise an error.
' If Abbreviate is True, the returned name is abbreviated.
'
' 2015-11-25. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function MonthNameInvariant( _
    ByVal Month As Long, _
    Optional ByVal Abbreviate As Boolean) _
    As String
    
    Const AbbreviatedLength As Integer = 3
    
    Dim MonthName( _
        MinMonthValue To _
        MaxMonthValue)      As String
    Dim Name                As String
    
    If Not IsMonth(Month) Then
        Err.Raise DtError.dtInvalidProcedureCallOrArgument
        Exit Function
    End If
    
    ' Non-localized (invariant) month names.
    MonthName(1) = "January"
    MonthName(2) = "February"
    MonthName(3) = "March"
    MonthName(4) = "April"
    MonthName(5) = "May"
    MonthName(6) = "June"
    MonthName(7) = "July"
    MonthName(8) = "August"
    MonthName(9) = "September"
    MonthName(10) = "October"
    MonthName(11) = "November"
    MonthName(12) = "December"
    
    If Abbreviate = True Then
        Name = Left(MonthName(Month), AbbreviatedLength)
    Else
        Name = MonthName(Month)
    End If
    
    MonthNameInvariant = Name

End Function

taken from module DateText.bas from my project enter image description here

  • Related