MS Access - 按月搜索条件

时间:2016-03-30 17:53:39

标签: sql vba ms-access

我正在努力创造一个月的时间。以下SQL代码的年搜索条件。我正在制作一个MS Access应用程序,让用户选择公司 - 我已经完成了。然后让用户选择月份&然后用户点击确定并获得过滤月份的信息以及前2个月的信息。我知道这很多,但我对日期过滤器没有运气。我已经遵循了各种各样的例子,但我似乎无法理解它。有人可以帮我解决一个代码示例或类似的东西。非常感谢

这是SQL代码:

TRANSFORM Sum(dbo_ASSET_HISTORY.MARKET_VALUE) AS SumOfMARKET_VALUE
SELECT [dbo_FIRM]![NAME] AS [FIRM NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME,dbo_FUND.PRODUCT_NAME                                     
FROM (dbo_ASSET_HISTORY INNER JOIN dbo_FIRM ON dbo_ASSET_HISTORY.FIRM_ID = dbo_FIRM.FIRM_ID) INNER JOIN dbo_FUND ON dbo_ASSET_HISTORY.FUND = dbo_FUND.FUND
WHERE (((dbo_FIRM.NAME) Like "Voya F*") AND     ((dbo_ASSET_HISTORY.ASSET_YEAR)=Year(DateAdd('m',-1,Date()))) AND     ((dbo_ASSET_HISTORY.ASSET_MONTH) In (Month(DateAdd('m',-1,Date())))))
GROUP BY [dbo_FIRM]![NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME
PIVOT [dbo_ASSET_HISTORY]![ASSET_YEAR] & '-' & [dbo_ASSET_HISTORY]!    [ASSET_MONTH];

这是一块VBA 这只是“月过滤器”,只是作为测试是否有效

Dim adjust As String
Dim task As String

Private Sub Command0_Click()
If IsNull(Me.month_filt) Then
    DoCmd.Close
    DoCmd.OpenQuery "ct_VoysF_AUM_Q"
Else
    adjust = "(ASSET_MONTH >= #" & Me.month_filt & "# AND ASSET_MONTH <= #"    & Me.month_filt & "#)"
    task = "Select * from ct_VoysF_AUM_Q where (" & adjust & ")"
    DoCmd.ApplyFilter task
End If
End Sub

1 个答案:

答案 0 :(得分:1)

由于您的月份与月份匹配,因此您不使用# - 这仅适用于日期:

adjust = "(ASSET_MONTH >= " & Me.month_filt & " AND #ASSET_MONTH <= " & Me.month_filt & ")"

仅 OR

adjust = "(ASSET_MONTH BETWEEN " & Me.month_filt & " AND " & Me.month_filt & ")"