私はvbaとsqlにまったく慣れていないので、vbaを介してデータベースクエリからExcelにデータをインポートするのに少し問題があります。
クエリ結果を問題なく表示するAccessでクエリを生成する次のSQLがあります。
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]))' を集計関数の一部として使用します。"
誰かが私を正しい方向に向けることができれば、または私がこれを完全に間違っているなら、私はとても感謝しています!