-1

我对 vba 和 sql 还是很陌生,我在通过 vba 将数据库查询中的一些数据导入到 excel 中时遇到了一些麻烦。

我有以下 sql 在 Access 中生成查询,显示查询结果没问题。

SELECT  tblSection.Section_Name, 
        IIf(Sum(qry_Total_Assigned_MB.[Total Assigned Main Book]) Is Null,0,
            Sum(qry_Total_Assigned_MB.[Total Assigned Main Book])) As [Total MB Assigned], 
        IIf(Sum([qry_Total_Shoot_Complete_MB].[Total MB Product Shoot Complete]) Is Null,0,
            Sum([qry_Total_Shoot_Complete_MB].[Total MB Product Shoot Complete])) As [Total MB Product Shoot Complete], 
        IIf(Sum(qry_Total_Assigned_MidB.[Total Assigned Mid Book]) Is Null,0,
            Sum(qry_Total_Assigned_MidB.[Total Assigned Mid Book])) As [Total MidB Assigned], 
        IIf(Sum([qry_Total_Shoot_Complete_MidB].[Total MidB Product Shoot Complete]) Is Null,0,
            Sum([qry_Total_Shoot_Complete_MidB].[Total MidB Product Shoot Complete])) As [Total MidB Product Shoot Complete], 
        IIf(Sum([qry_Total_Assigned_ECA].[Total Assigned ECA]) Is Null,0,
            Sum([qry_Total_Assigned_ECA].[Total Assigned ECA])) As [Total ECA Assigned], 
        IIf(Sum([qry_Total_Assigned_ECOMM].[Total Assigned ECOMM]) Is Null,0,
            Sum([qry_Total_Assigned_ECOMM].[Total Assigned ECOMM])) As [Total ECOMM Assigned], 
        IIf(Sum([qry_Total_Assigned_Marketing].[Total Assigned Marketing]) Is Null,0,
            Sum([qry_Total_Assigned_Marketing].[Total Assigned Marketing])) As [Total Marketing Assigned], 
        IIf([Total MB Assigned]=0,"0",IIf([Total MB Product Shoot Complete]=0,"0",
            ([Total MB Product Shoot Complete])/([Total MB Assigned]))) As [Percentage MB Complete] 

FROM    ((((((tblSection 
        LEFT JOIN qry_Total_Assigned_MB
            ON tblSection.Section_Name = qry_Total_Assigned_MB.Section_Name) 
        LEFT JOIN qry_Total_Assigned_MidB 
            ON tblSection.Section_Name = qry_Total_Assigned_MidB.Section_Name) 
        LEFT JOIN qry_Total_Assigned_ECA 
            ON tblSection.Section_Name = qry_Total_Assigned_ECA.Section_Name) 
        LEFT JOIN qry_Total_Assigned_ECOMM 
            ON tblSection.Section_Name = qry_Total_Assigned_ECOMM.Section_Name) 
        LEFT JOIN qry_Total_Assigned_Marketing 
            ON tblSection.Section_Name = qry_Total_Assigned_Marketing.Section_Name) 
        LEFT JOIN qry_Total_Shoot_Complete_MB 
            ON tblSection.Section_Name = qry_Total_Shoot_Complete_MB.Section_Name) 
        LEFT JOIN qry_Total_Shoot_Complete_MidB 
            ON tblSection.Section_Name = qry_Total_Shoot_Complete_MidB.Section_Name 
GROUP BY tblSection.Section_Name;

然后,我尝试使用 vba 将使用以下 sql 语句通过 vba 生成的查询中的记录数据提取到 excel 中:

SELECT [Total MB Assigned] 
FROM qry_MI_Total_Assigned 
WHERE [Section_Name] = '" & fnSection_Name & "' 
GROUP BY [Section_Name];

运行上述代码时,出现以下错误:

您厌倦了执行不包含指定表达式的查询 'IIf(IIf(Is Null,0,)=0,"0", IIf([Total MB Product Shoot Complete]=0, "0",[Total MB Product Shoot Complete]/[Total MB Assigned]))' 作为聚合函数的一部分。”

如果有人能指出我正确的方向,或者如果我这样做完全错误,我将非常感激!

4

1 回答 1

0

在以下查询中,您的错误消息是不言自明的:

SELECT [Total MB Assigned] 
FROM qry_MI_Total_Assigned 
WHERE [Section_Name] = '" & fnSection_Name & "' 
GROUP BY [Section_Name];

[Total MB Assigned]在选择列表中,但不在聚合函数或分组依据的一部分中。由于qry_MI_Total_Assigned是存储查询,因此错误消息只是为您提供了该列的完整定义。

由于查询qry_MI_Total_Assigned已经分组,[Section_Name]我认为您GROUP BY是多余的,并且以下内容将按您的预期工作

SELECT [Total MB Assigned] 
FROM qry_MI_Total_Assigned 
WHERE [Section_Name] = '" & fnSection_Name & "'
于 2013-09-02T10:31:21.363 回答