I am encountering a problem in VBA, where the execution of an SQL-query generates Run-time Error 3075.
Running the same query in Microsoft SQL Server Manager, I encounter no problems and the desired result is returned.
I tried printing the sql-query with Debug.print (as suggested here: https://stackoverflow.com/a/3136299/4474367) to see what the query looks like - it looks ok and is working perfectly fine pasted into SQL Server Manager.
What I'm doing is selecting total count of rows (survey responses) for a certain Dealer (and some other criteria), dividing the result with the total amount of rows where A2 = 5 (which is selected with a subquery) and then multiplying with 100 to get percentage of A2 = 5.
What do you guys think is wrong? Are there any limitations in Access that I may not have taken consideration of? Only the sub-query is shown in the error-message.
Here is my query (I cut it up a little with line breaks to make it easier for reading):
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;
Uncut version (exactly the same as above, but without line breaks):
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 Snippet, query procedure:
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
Here is a link to an image of the error I receive
Best Regards and thanks for reading!