我有一个用于输入数据的表单。排序顺序,开始日期和结束日期。
输入数据后,将生成报告。但我得到了错误。
错误:“Microsoft Access数据库引擎无法识别'[Forms]![AuditPTETotals]![startDate]'作为有效的字段名称或表达式。
这是我的交叉表(AuditPTETotals_CrosstabDateRange)查询,它使用另一个查询检索的数据(AuditPTETotalsDateRange)
TRANSFORM Sum(AuditPTETotalsDateRange.PTEtotal) AS SumOfPTEtotal
SELECT AuditPTETotalsDateRange.companyName, AuditPTETotalsDateRange.regNum, Sum(AuditPTETotalsDateRange.PTEtotal) AS [Total Of PTEtotal]
FROM AuditPTETotalsDateRange
GROUP BY AuditPTETotalsDateRange.companyName, AuditPTETotalsDateRange.regNum
PIVOT AuditPTETotalsDateRange.description;
这是AuditPTETotalsDateRange
SELECT CUSTOMER.regNum, CUSTOMER.companyName, ScrapTireType.description, ScrapTireType.PTEamount, ScrapCollectionTireType.amount, ScrapCollection.PTEtotal, ScrapCollection.Date
FROM (ScrapCollection INNER JOIN CUSTOMER ON ScrapCollection.regNum = CUSTOMER.regNum) 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]![startDate] And [Forms]![AuditPTETotals]![endDate]));
我以类似的方式使用这种方法来引用查询中的表单字段,但这一次,我不确定我做错了什么。请帮忙。
谢谢
答案 0 :(得分:1)
我找到了解决方案!我在控件上使用了日期/时间格式。我认为它搞乱了我的参数的实际格式。相反,我使用常规文本框并为其设置输入掩码。有效。
答案 1 :(得分:0)
在您正在使用的表单上创建一个按钮,并将下面的内容添加到其Click事件中。请务必将 MyReportName 更改为实际的报告名称。 MyReportName 在以下代码中出现两次。
On Error Resume Next
DoCmd.Close acReport, "MyReportName"
DoCmd.DeleteObject acQuery, "AuditPTETotalsDateRange"
On Error GoTo Err_cmd_Click
Dim strSQL, strSQL1 As String
Dim qdef As DAO.QueryDef
strSQL = "SELECT CUSTOMER.regNum, CUSTOMER.companyName, ScrapTireType.description, ScrapTireType.PTEamount, ScrapCollectionTireType.amount, ScrapCollection.PTEtotal, ScrapCollection.Date " & _
"FROM (ScrapCollection INNER JOIN CUSTOMER ON ScrapCollection.regNum = CUSTOMER.regNum) 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]![startDate] & "# And #" & [Forms]![AuditPTETotals]![endDate] & "#))"
strSQL1 = "TRANSFORM Sum(AuditPTETotalsDateRange.PTEtotal) AS SumOfPTEtotal " & _
"SELECT AuditPTETotalsDateRange.companyName, AuditPTETotalsDateRange.regNum, Sum(AuditPTETotalsDateRange.PTEtotal) AS [Total Of PTEtotal] " & _
"FROM AuditPTETotalsDateRange " & _
"GROUP BY AuditPTETotalsDateRange.companyName, AuditPTETotalsDateRange.regNum " & _
"PIVOT AuditPTETotalsDateRange.description"
Set qdef = CurrentDb.CreateQueryDef("AuditPTETotalsDateRange", strSQL)
Set qdef = CurrentDb.CreateQueryDef("AuditPTETotals_CrosstabDateRange", strSQL1)
DoCmd.OpenReport "MyReportName", acViewPreview
Exit_cmd_Click:
Exit Sub
Err_cmd_Click:
MsgBox Err.Description
Resume Exit_cmd_Click
然后在表格On Close事件中添加以下内容:
On Error Resume Next
DoCmd.DeleteObject acQuery, "AuditPTETotalsDateRange"
然后,如果单击该按钮,则会删除AuditPTETotalsDateRange,然后重新创建。此外,如果您关闭表单,则会删除AuditPTETotalsDateRange查询。这是好的,因为只有在表单打开时才需要查询。将报告的记录源设置为AuditPTETotals_CrosstabDateRange。
您必须设置 Microsoft DAO x.x Object Library 的引用才能使上述代码生效。您可能已经拥有参考集。 x.x代表版本。任何版本都应该有效。
修改强>
您可能希望在进行所有这些更改之前复制原始报告,只是因为它不适合您。尝试从报表上的字段中删除数据源,以便它们不受约束。然后将以下代码添加到关于打开事件的报告中:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("AuditPTETotals_CrosstabDateRange", dbOpenDynaset)
On Error Resume Next
[txtdata1].ControlSource = rs.Fields(4).Name
[txtData2].ControlSource = rs.Fields(5).Name
[txtData3].ControlSource = rs.Fields(6).Name
//And etc for as many fields as you have on report
您还可以以类似的方式在页眉上指定列标题。他们需要从标签更改为未绑定的文本框才能使其正常工作。将以下代码添加到页面标题格式化事件:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("AuditPTETotals_CrosstabDateRange", dbOpenDynaset)
On Error Resume Next
[txtColHeading1].value = rs.Fields(4).Name
[txtColHeading2].value = rs.Fields(5).Name
[txtColHeading3].value = rs.Fields(6).Name
//And etc for as many fields as you have on report
这将允许您的报告在不知道您将拥有哪些列的情况下运行。确保将控件命名为与代码中的名称相匹配,反之亦然。