ms-access:仅在特定日期内显示记录的报告

时间:2010-05-10 20:57:46

标签: ms-access vba

我有一个非常简单的报告,它只从一个表生成。表中的一列是日期。

我需要能够允许报告的用户输入一系列日期并仅在这些日期之间显示数据。

我该怎么做>?

3 个答案:

答案 0 :(得分:4)

我不喜欢在表格/报告的记录源中硬连接参数或表格引用,因此我会修改@ Remou的想法,而不是在报告的OnOpen事件中设置RecordSource。也就是说,首先打开表单,收集所选日期的值,然后将它们插入到报表的RecordSource的where子句中。像这样的东西(从我的真实报告中复制):

  Dim strRecordSource As String

  DoCmd.OpenForm "dlgDateRange", , , , , acDialog, "ThisYear"
  If IsLoaded("dlgDateRange") Then
     With Forms!dlgDateRange
       If .Tag = "Cancel" Then
          Cancel = True
       Else
          Me.Filter = "[InvoiceDate] Between #" & !txtStart & "# AND #" & !txtEnd & "#"
          Me.FilterOn = True
          Me!lblDateRange.Caption = StrConv(Trim(("from " + varZLStoNull(Format(!txtStart, "mm/dd/yyyy"))) & (" to " + varZLStoNull(Format(!txtEnd, "mm/dd/yyyy")))), vbProperCase)
       End If
     End With
     DoCmd.Close acForm, "dlgDateRange"
  End If

一些意见:

  • 此处调用的对话框形式比您需要的复杂得多,因为它有一堆预定义的日期范围,根据对话框窗体的OpenArgs参数设置。表格如下:

alt text http://dfenton.com/DFA/examples/DateCriteriaForm.png

我使用Stephan Lebans date picker code允许用户从日历控件中选择日期。

设置日期范围的代码是这个,我所要做的就是传递一个CASE SELECT的案例:

  Public Sub SetDates(strType As String, ctlStart As Control, ctlEnd As Control)
    Dim dteStart As Date
    Dim dteEnd As Date
    Dim ctl As Control

    Select Case strType
      Case "EndOnly" ' OK
        dteStart = #1/1/1980#
        ctlStart.Enabled = False
        dteEnd = Date
      Case "Trace" ' OK
        dteStart = DateAdd("d", -7, Date)
        dteEnd = DateAdd("d", 7, Date)
      Case "LastWeek" ' OK
        dteStart = Date - Weekday(Date, vbMonday) - 6
        dteEnd = dteStart + 6
      Case "ThisWeek" ' OK
        dteStart = Date - Weekday(Date, vbMonday) + 1
        dteEnd = dteStart + 6
      Case "LastMonth" ' OK
        dteStart = month(DateAdd("m", -1, Date)) & "/01/" & year(DateAdd("m", -1, Date))
        dteEnd = DateAdd("m", 1, dteStart) - 1
      Case "ThisMonth" ' OK
        dteStart = month(Date) & "/01/" & year(Date)
        dteEnd = DateAdd("m", 1, dteStart) - 1
      Case "LastQuarter" ' OK
        dteStart = DateSerial(year(DateAdd("q", -1, Date)), (3 * Format(DateAdd("q", -1, Date), "q")) - 2, 1)
        dteEnd = DateAdd("q", 1, dteStart) - 1
      Case "ThisQuarter" ' OK
        dteStart = DateSerial(year(Date), (3 * Format(Date, "q")) - 2, 1)
        dteEnd = DateAdd("q", 1, dteStart) - 1
      Case "LastYear" ' OK
        dteStart = "01/01/" & year(Date) - 1
        dteEnd = "12/31/" & year(Date) - 1
      Case "ThisYear" ' OK
        dteStart = "01/01/" & year(Date)
        dteEnd = "12/31/" & year(Date)
      Case "LastFY" ' OK
        dteStart = "09/01/" & year(DateAdd("m", 4, Date)) - 2
        dteEnd = DateAdd("yyyy", 1, dteStart) - 1
      Case "ThisFY" ' OK
        dteStart = "09/01/" & year(DateAdd("m", 4, Date)) - 1
        dteEnd = DateAdd("yyyy", 1, dteStart) - 1
      Case "Last3Years" ' OK
        dteStart = "01/01/" & year(Date) - 2
        dteEnd = Date
      Case "BeforeLast3Years" ' OK
        dteEnd = DateValue("01/01/" & year(Date) - 2) - 1
      Case Else
        dteStart = Date
        dteEnd = Date
    End Select
    If ctlStart.Enabled Then
       If dteStart = 0 Then
          ctlStart = Null
       Else
          ctlStart = Format(dteStart, "mm/dd/yyyy")
       End If
    End If
    If ctlEnd.Enabled Then
       If dteEnd = 0 Then
          ctlEnd = Null
       Else
          ctlEnd = Format(dteEnd, "mm/dd/yyyy")
       End If
    End If
    For Each ctl In ctlStart.Parent!optPresetDates.Controls
      If ctl.ControlType <> acLabel Then
         If Replace(ctl.Controls(0).Caption, " ", vbNullString) = strType Then
            ctlStart.Parent!optPresetDates = ctl.OptionValue
            Exit For
         End If
      End If
    Next ctl
    Set ctl = Nothing
  End Sub

这确实比你需要的信息更多,但我想说的是你应该考虑将你的报告的记录源绑定到参数或对话框表格。

答案 1 :(得分:1)

最好的解决方案可能是创建一个允许用户输入日期的小表单。然后,报表所基于的查询可以参考表单:

SELECT f1,f2,f3 FROM Table 
WHERE SomeDate 
BETWEEN Forms!DateSelect!StartDate AND Forms!DateSelect!EndDate

答案 2 :(得分:1)

我要做的是创建一个选择表中所有行的查询,并为日期字段设置几个参数。请尝试以下方法:

  1. 在查询设计中指定日期字段:

    格式([YourDateField],“mmm.dd,yyyy”)

  2. 对于标准写:

    格式之间([From],“mmm.yy,dddd”)和格式([To],“mmm.dd.yyyy”)

  3. 运行查询时,应出现两个输入框,询问指定格式的FromTo日期。