【问题标题】:Access Previous Month Query访问上月查询
【发布时间】:2021-12-05 21:01:17
【问题描述】:

创建查询以仅从表中提取上个月的数据。我的任何研究都没有运气,只是试图仅从一个月列中提取数据。该表包括以下字段: 月份、类型、完成和进行中

月份字段列出一月、三月等。

仅尝试在设计视图字段中添加条件。任何帮助,将不胜感激。谢谢。

例子

表:[完成月份/IP 和总和] 月份类型 完成中 9 月 1 日 10 日 2 9 月 2 日 3 0 9 月 3 日 11 日 6 十月 1 1 1 10月2日6月7日

在本月(10 月)运行查询后,结果如下: 月份类型 完成中 9 月 1 日 10 日 2 9 月 2 日 3 0 9月3日11日6

【问题讨论】:

    标签: database ms-access


    【解决方案1】:

    可能是:

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

    使用函数:

    ' 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
    

    取自模块DateText.bas,来自我的项目VBA.Date

    【讨论】:

    • 对不起 Gustav 但这不是 VBA 的,我没有这方面的经验。这只是简单的 Access 2016
    【解决方案2】:

    下面的查询呢?如果您提供一些示例数据和预期输出会更好。

    SELECT Month, Type, Done, InProgress
    FROM Table3
    WHERE (((Table3.Month)=Format$(DateSerial(Year(Date()),Month(Date())-1,Day(Date())),"mmmm")));
    

    【讨论】:

    • Harun24HR,我尝试了您的查询的一个版本,至少最终得到了一些结果。
    • SELECT [Month Done/IP and Sum].[Month #], [Month Done/IP and Sum].Month, [Month Done/IP and Sum].Type, [Month Done/IP and Sum].Done, [Month Done/IP and Sum].[In Progress] FROM [Month Done/IP and Sum] WHERE (([Month Done/IP and Sum].[Month #]=Month(Date() -1)));问题是我现在才从这个月得到结果,而不是没有结果或有所有结果。我只想从上个月提取结果。我有什么遗漏或可以调整的吗?
    【解决方案3】:

    终于明白了,Harun24HR谢谢你的帮助。只是需要一个起点。我需要创建另一个以月份为数字的字段。

    我需要的 SQL 查询拉取结果: SELECT [Month Done/IP and Sum].Month, [Month Done/IP and Sum].Type, [Month Done/IP and Sum].Done, [Month Done/IP and Sum].[In Progress] FROM [完成月份/IP 和总和] WHERE ((([Month Done/IP and Sum].[Month #])=Month(Date())-"1"));

    【讨论】:

    • 如果能把答案详细解释给以后的读者看就更好了。
    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多