我对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([总MB产品拍摄完成] = 0,“0”的查询) [总MB产品拍摄完成] / [已分配的总MB数]))'作为聚合函数的一部分。“
如果有人能指出我正确的方向,或者如果我这样做完全错了,我将非常感激!
答案 0 :(得分: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 & "'