表单字段的MS Access参考问题

时间:2012-06-06 12:06:58

标签: vba ms-access

嗨,这是我的目标,我有一个带有开始日期(start_Date)和结束日期(end_Date)字段的for和一个有交叉表查询作为记录源的报告。我希望能够根据表单上给出的日期对报表中的数据进行排序。我尝试了3种不同的方法。

  1. 运行宏并打开带有where条件的报告,在这种情况下,Access会抱怨并说“[Reports]![AuditPTETotal]![Date]不是有效的参考”

  2. 通过VBA设置报告的“where condition”。另一个引用错误,但这一次,它表示我的表单字段无效。

      Private Sub ViewReport_Click()
    
        Dim strWhere As String
    
        If Not IsNull(Me.start_Date) Then
          strWhere = strWhere & " AND [Reports]![AuditPTETotal]![Date] >=#" & [Forms]! [AuditPTETotals]![start_Date] & "# "
        End If
        If Not IsNull(Me.end_Date) Then
          strWhere = strWhere & " AND [Reports]![AuditPTETotal]![Date] <=#" & [Forms]!   [AuditPTETotals]![end_Date] & "# "
        End If
        DoCmd.OpenReport "AuditPTETotal", acViewNormal, , strWhere
    
       End Sub 
    
  3. 最后,将表单值传递给查询。这些方法都不适用于我,我真的需要完成这项工作。我不能选择。任何帮助,将不胜感激。

  4. 查询

    PARAMETERS [Forms]![AuditPTETotals]![start_Date] DateTime, [Forms]![AuditPTETotals]!   [end_Date] DateTime;
    TRANSFORM Sum(ScrapCollection.PTEtotal) AS SumOfPTEtotal
    SELECT ScrapCollection.Date, ScrapCollection.regNum
    FROM ScrapCollection INNER JOIN (ScrapTireType INNER JOIN ScrapCollectionTireType ON (ScrapTireType.scrapTireTypeID = ScrapCollectionTireType.scrapTireTypeID) AND (ScrapTireType.scrapTireTypeID = ScrapCollectionTireType.scrapTireTypeID)) ON ScrapCollection.scrapCollectionID = ScrapCollectionTireType.scrapCollectionID
    WHERE (((ScrapCollection.Date) Between [Forms]![AuditPTETotals]![start_Date] And [Forms]![AuditPTETotals]![end_Date]))
    GROUP BY ScrapCollection.Date, ScrapCollection.regNum
    PIVOT ScrapTireType.description;
    

1 个答案:

答案 0 :(得分:1)

如果要使用表单限制交叉表的结果,则必须具有参数:

PARAMETERS [Forms]![AForm]![text0] DateTime, [Forms]![AForm]![text2] DateTime;
TRANSFORM Count(Table1.AKey) AS CountOfAKey
SELECT Table1.AText
FROM Table1
WHERE (((Table1.ADate) Between [Forms]![AForm]![text0] And [Forms]![AForm]![text2]))
GROUP BY Table1.AText
PIVOT Table1.ADate;