MS Access VBA SQL 使用子查询查询时出现运行时错误 3075
MS Access VBA Runtime error 3075 on SQL Query with subquery
我在 VBA 中遇到问题,执行 SQL-query 会生成 Run-time 错误 3075。
运行 Microsoft SQL 服务器管理器中的相同查询,我没有遇到任何问题,并返回了所需的结果。
我尝试使用 Debug.print 打印 sql-query(如此处建议:)以查看查询的外观 - 它看起来不错并且工作得很好,粘贴到 SQL服务器管理器。
我正在做的是为某个经销商(和其他一些标准)选择总行数(调查响应),将结果除以总行数,其中 A2 = 5(用子查询)然后乘以 100 得到 A2 的百分比 = 5.
大家觉得哪里不对? Access 中是否有任何我可能没有考虑到的限制? error-message中只显示了sub-query。
这是我的查询(为了便于阅读,我用换行符将其删减):
SELECT DealerCode,
(SELECT CAST(COUNT(A2) AS FLOAT)
FROM dbo_Service
WHERE A2 = 5
AND MarketCode = 'CA'
AND DealerCode = '3140'
AND CompleteDate BETWEEN '2014-01-20'
AND '2015-01-20') / COUNT(*) * 100 AS TopBox
FROM dbo_Service
WHERE DealerCode = '3140'
AND MarketCode = 'CA'
AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20'
GROUP BY DealerCode;
未删减版(和上面一模一样,只是没有换行):
SELECT DealerCode, (SELECT CAST(COUNT(A2) AS FLOAT) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;
VBA 代码段,查询过程:
strSQL = "SELECT DealerCode, (SELECT CAST(COUNT(A2) AS FLOAT) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
topBox = rs!topBox
db.Close
这是我收到的错误的 link 图片
致以最诚挚的问候,感谢阅读!
那是 SQL 服务器方言查询,不是 MS Access。您需要在 SQL 服务器上执行查询,传递查询。有关详细信息和解决方案,请参阅 SQL Server Passthrough query stored into an Access VBA recordset。
因此,在遵循 HansUp 的回答后,我编辑了我的 SQL-查询并将 CAST 替换为 Cdbl。
现在看起来像这样:
"SELECT DealerCode, ((SELECT CDbl(COUNT(A2)) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*)) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;"
当更改为 Cdbl 而不是 CAST 时,我还必须在 CAST 之后删除 "AS FLOAT"。
简而言之,它看起来像这样:
SELECT CDbl(COUNT(A2))
而不是这个:
SELECT CAST(COUNT(A2) AS FLOAT)
完成工作的快速修复!
感谢您的回答!
我在 VBA 中遇到问题,执行 SQL-query 会生成 Run-time 错误 3075。
运行 Microsoft SQL 服务器管理器中的相同查询,我没有遇到任何问题,并返回了所需的结果。
我尝试使用 Debug.print 打印 sql-query(如此处建议:)以查看查询的外观 - 它看起来不错并且工作得很好,粘贴到 SQL服务器管理器。
我正在做的是为某个经销商(和其他一些标准)选择总行数(调查响应),将结果除以总行数,其中 A2 = 5(用子查询)然后乘以 100 得到 A2 的百分比 = 5.
大家觉得哪里不对? Access 中是否有任何我可能没有考虑到的限制? error-message中只显示了sub-query。
这是我的查询(为了便于阅读,我用换行符将其删减):
SELECT DealerCode,
(SELECT CAST(COUNT(A2) AS FLOAT)
FROM dbo_Service
WHERE A2 = 5
AND MarketCode = 'CA'
AND DealerCode = '3140'
AND CompleteDate BETWEEN '2014-01-20'
AND '2015-01-20') / COUNT(*) * 100 AS TopBox
FROM dbo_Service
WHERE DealerCode = '3140'
AND MarketCode = 'CA'
AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20'
GROUP BY DealerCode;
未删减版(和上面一模一样,只是没有换行):
SELECT DealerCode, (SELECT CAST(COUNT(A2) AS FLOAT) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;
VBA 代码段,查询过程:
strSQL = "SELECT DealerCode, (SELECT CAST(COUNT(A2) AS FLOAT) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;"
Debug.Print strSQL
Set rs = db.OpenRecordset(strSQL)
topBox = rs!topBox
db.Close
这是我收到的错误的 link 图片
致以最诚挚的问候,感谢阅读!
那是 SQL 服务器方言查询,不是 MS Access。您需要在 SQL 服务器上执行查询,传递查询。有关详细信息和解决方案,请参阅 SQL Server Passthrough query stored into an Access VBA recordset。
因此,在遵循 HansUp 的回答后,我编辑了我的 SQL-查询并将 CAST 替换为 Cdbl。
现在看起来像这样:
"SELECT DealerCode, ((SELECT CDbl(COUNT(A2)) FROM dbo_Service WHERE A2 = 5 AND MarketCode = 'CA' AND DealerCode = '3140' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20') / COUNT(*)) * 100 AS TopBox FROM dbo_Service WHERE DealerCode = '3140' AND MarketCode = 'CA' AND CompleteDate BETWEEN '2014-01-20' AND '2015-01-20' GROUP BY DealerCode;"
当更改为 Cdbl 而不是 CAST 时,我还必须在 CAST 之后删除 "AS FLOAT"。
简而言之,它看起来像这样:
SELECT CDbl(COUNT(A2))
而不是这个:
SELECT CAST(COUNT(A2) AS FLOAT)
完成工作的快速修复!
感谢您的回答!