我正在使用以下代码将交叉表查询导出到excel。
我尝试过Set rs1 = CurrentDb.OpenRecordset("qry_Comparison_Bulk")
,但总是收到错误消息:参数太少。预期为19。该查询中存储了19个参数。我的下一个想法是仅在VBA中重新创建SQL。现在出现错误:交叉表列标题太多(424)。 424是数据库中的项目总数,因此我认为过滤器无法正常工作。
Set rs1 = CurrentDb.OpenRecordset("TRANSFORM Sum(tbl_Formulas.Input) AS SumOfInput " _
& "SELECT tbl_Formulas.BillType, tbl_Formulas.RawMaterial " _
& "FROM tbl_Formulas " _
& "WHERE (tbl_Formulas.BillType)='" & [Forms]![frm_BulkComparison_Select]![bill1] & "' Or (tbl_Formulas.BillType)='" & [Forms]! [frm_BulkComparison_Select]![bill2] & "' Or (tbl_Formulas.BillType)='" & [Forms]![frm_BulkComparison_Select]![bill3] & "' AND (tbl_Formulas.Item)='" &[Forms]![frm_BulkComparison_Select]![Item 1] & "' Or " _
& "(tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 2] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 3] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 4] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 5] & "' Or " _
& "(tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 6] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 7] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 8] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 9] & "' Or " _
& "(tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 10] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 11] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 12] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 13] & "' Or " _
& "(tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 14] & "' Or (tbl_Formulas.Item)='" & [Forms]![frm_BulkComparison_Select]![Item 15] & "' AND (tbl_Formulas.BP)='" & [Forms]![frm_BulkComparison_Select]![BP] & "' " _
& "GROUP BY tbl_Formulas.BillType, tbl_Formulas.RawMaterial " _
& "PIVOT tbl_Formulas.Item;")
有“'&...&“'分隔符,可避免使用参数,因为每个字段的值都是文本。
此外,我的表单设置有15个用于“项目”的文本框,3个用于“ BillType”的文本框和1个用于“ BP”的文本框
答案 0 :(得分:2)
我猜您的qry_Comparison_Bulk
查询引用了类似tbl_Formulas.BillType=[Forms]![frm_BulkComparison_Select]![bill1]
的表单控件
并且该查询在从Access查询设计器运行时有效。问题是,当您尝试将命名查询与OpenRecordset
一起使用时,这些控件中的每一个都被视为没有为其提供值的参数。
因此请提供值...
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Dim rs As DAO.Recordset
Set qdf = CurrentDb.QueryDefs("qry_Comparison_Bulk")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next
Set rs = qdf.OpenRecordset()
prm.Name
将被Access解释为参数名...,例如[Forms]![frm_BulkComparison_Select]![bill1]
...因此Eval(prm.Name)
等效于Eval([Forms]![frm_BulkComparison_Select]![bill1])
...并返回该控件的值...然后将其分配给prm.Value
答案 1 :(得分:1)
我认为您采用第一种方法可能会更好:在获取记录集之前,只需在查询中设置参数即可:
Public Sub ProcessQuery()
Dim db As DAO.Database, qdf As DAO.QueryDef, rs As DAO.Recordset
Set db = CurrentDb
Set qdf = db.QueryDefs("qry_Comparison_Bulk")
' set parameters
qdf.Parameters(1).value = [Forms]![frm_BulkComparison_Select]![Item 1]
Set rs1 = qdf.OpenRecordset
End Sub