MS Access将联合查询导出到Excel,VBA问题

时间:2018-12-27 07:40:57

标签: ms-access access-vba export-to-excel ms-access-2016 union-all

我不是程序员,但是我正在为工作建立一个Access数据库。 该数据库现在可以正常工作,但是我正在努力将一个重要的查询导出为Excel。感谢您的帮助,对于不好的做法以及可能出现的许多语法错误,我们深表歉意。

从下面名为“直到今天的返工流程”的联合查询中,我创建了一个查询来按产品代码分组,命名为“直到今天的返工流程查询”,然后创建了另一个查询以添加列以进行一些计算,我希望导出到其中Excel。

我需要导出的查询名为“导出查询”。看起来像这样:

(产品/开始返工库存/二手返工/新返工/总返工)

这些是根据“联合查询”和“分组查询”中获得的数据计算得出的字段。

这是联合查询代码:

SELECT DISTINCTROW 
Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Rework Used] AS [Rework Used TODAY lb 1], Batch.[Support row] AS [Rework Used YESTERDAY lb 1], 
Batch.[Support row] AS [Rework Used TODAY lb 2], 
Batch.[Support row] AS [Rework Used YESTERDAY lb 2], 
Batch.[Support row] AS [New Rework TODAY lb], 
Batch.[Support row] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.[Rework Product] = Products.[Product ID]
WHERE (((Batch.[Production Date])=[Forms]![01 Production Batch Form]![Today Date]))

UNION ALL

SELECT DISTINCTROW 
Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Support row] AS [Rework Used TODAY lb 1], 
Batch.[Rework Used] AS [Rework Used YESTERDAY lb 1], 
Batch.[Support row] AS [Rework Used TODAY lb 2], 
Batch.[Support row] AS [Rework Used YESTERDAY lb 2], 
Batch.[Support row] AS [New Rework TODAY lb], 
Batch.[Support row] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.[Rework Product] = Products.[Product ID]
WHERE (((Batch.[Production Date])<[Forms]![01 Production Batch Form]![Today Date]))

UNION ALL

SELECT DISTINCTROW 
Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Support column] AS [Rework Used TODAY lb 1], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 1], 
Batch.[Rework Used 2] AS [Rework Used TODAY lb 2], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 2], 
Batch.[Support column] AS [New Rework TODAY lb], 
Batch.[Support column] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.[Rework Product 2] = Products.[Product ID]
WHERE (((Batch.[Production Date])=[Forms]![01 Production Batch Form]![Today Date]))

UNION ALL

SELECT DISTINCTROW 
Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Support column] AS [Rework Used TODAY lb 1], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 1], 
Batch.[Support column] AS [Rework Used TODAY lb 2], 
Batch.[Rework Used 2] AS [Rework Used YESTERDAY lb 2], 
Batch.[Support column] AS [New Rework TODAY lb], 
Batch.[Support column] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.[Rework Product 2] = Products.[Product ID]
WHERE (((Batch.[Production Date])=[Forms]![01 Production Batch Form]![Today Date]))

UNION ALL

SELECT DISTINCTROW Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Support column] AS [Rework Used TODAY lb 1], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 1], 
Batch.[Rework Used 2] AS [Rework Used TODAY lb 2], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 2], 
Batch.[New Rework] AS [New Rework TODAY lb], 
Batch.[Support column] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.Product = Products.[Product ID]
WHERE (((Batch.[Production Date])=[Forms]![01 Production Batch Form]![Today Date]))

UNION ALL 

SELECT DISTINCTROW 
Batch.[Production Date], Products.[Rework Family] AS [Product Code], 
Batch.[Support column] AS [Rework Used TODAY lb 1], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 1], 
Batch.[Rework Used 2] AS [Rework Used TODAY lb 2], 
Batch.[Support column] AS [Rework Used YESTERDAY lb 2], 
Batch.[Support column] AS [New Rework TODAY lb], 
Batch.[New Rework] AS [New Rework YESTERDAY lb]
FROM Batch INNER JOIN Products ON Batch.Product = Products.[Product ID]
WHERE (((Batch.[Production Date])<[Forms]![01 Production Batch Form]![Today Date]));`

当我在代码中使用“联合查询”导出到excel时,我在日期前后添加了#“&和&”#,但出现错误号3129。 当我在代码中使用“导出查询”时,出现错误3061,参数太少,预期为1。

这是导出查询代码:

SELECT DISTINCTROW 
[Rework Flow until Today].[Product Code], 
Sum([Rework Flow until Today].[Rework Used TODAY lb 1]) AS [SumOfRework Used TODAY lb 1], 
Sum([Rework Flow until Today].[Rework Used YESTERDAY lb 1]) AS [SumOfRework Used YESTERDAY lb 1], 
Sum([Rework Flow until Today].[Rework Used TODAY lb 2]) AS [SumOfRework Used TODAY lb 2], 
Sum([Rework Flow until Today].[Rework Used YESTERDAY lb 2]) AS [SumOfRework Used YESTERDAY lb 2], 
Sum([Rework Flow until Today].[New Rework TODAY lb]) AS [SumOfNew Rework TODAY lb], 
Sum([Rework Flow until Today].[New Rework YESTERDAY lb]) AS [SumOfNew Rework YESTERDAY lb]
FROM [Rework Flow until Today]
GROUP BY [Rework Flow until Today].[Product Code];

下面是我的VBA代码

Dim SQL As String
Dim rs1 As DAO.Recordset
SQL = " One of the queries here "
Set rs1 = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)

谢谢

1 个答案:

答案 0 :(得分:0)

使用具有动态参数的查询打开记录集的VBA将不起作用,也不能使用序列中的任何查询具有动态参数的VBA。不要在查询中放入过滤条件,而将其放入打开记录集的代码中。

SQL = "SELECT * FROM queryname WHERE [Production Date]=#" & [Forms]![01 Production Batch Form]![Today Date] & "#"

不幸的是,[生产日期]字段在最终查询中不可用。替代选项:
1. TransferSpreadsheet
2.将记录写入“临时”表-表是永久性的,记录是临时的
3.导出将最终查询用作其RecordSource的报表对象