0

I'm looking to create an SQL query that will return the Case Manager, total number of activity & count of description that begins with 'Outgoing Calls'.

At the moment, I run this and get an error message saying Method 'Open' of object'_Recordset' failed. I'm sure it's because of 'Outgoing Calls' because it works fine when I remove it.

Can anyone see why this is failing, or know any alternative way of counting the number of descriptions that begin with Outgoing Calls? Bear in mind that Case method does not work on VBA.

SELECT m.CaseManager, 
SUM(m.Lead) AS 'Total Leads', 
COUNT(a.Description) AS 'Total Activity', 
COUNT(IF a.Description LIKE 'Outgoing Calls%' THEN 1 ENDIF) AS 'Outbound Calls' 
FROM AccountTable m INNER JOIN ActivityTable a 
ON m.AccountNumber=a.AccountNumber 
GROUP BY m.CaseManager;
1
  • Try executing the query directly in Access instead of from VBA code: you might get a more meaningful error message. The recordset is failing to open because of a syntax error in the SQL; the SQL editor should give better clues about what's wrong with it. Commented Mar 31, 2021 at 13:51

1 Answer 1

1

(1) The statement if IIF, not IF (2) The IIF-statement needs an ELSE-branch

cannot test it for the moment, but statement should look like

SUM(IIF(a.Description LIKE 'Outgoing Calls*', 1, 0)) AS 'Outbound Calls' 

https://support.microsoft.com/en-us/office/iif-function-32436ecf-c629-48a3-9900-647539c764e3

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks - I'm unfortunately seeing the same error message as above when I add this.
@DannyO'Sullivan try IIF(a.Description LIKE 'Outgoing Calls%',1,0). IIF is a function that takes arguments, not a statement. You're mixing up IIF function and SELECT CASE statements; CASE WHEN a.Description LIKE 'Outgoing Calls%' THEN 1 ELSE 0 END should also work (note: I don't know Access-SQL).
Sorry, mixed things up. Updated my answer
Thanks guys. Oddly enough, the query above returns the total number of activities rather than just the Outgoing calls. So essentially the IIF statement returns the same as COUNT(a.Description). (And I can confirm that it's not supposed to be the same count.
You need to use SUM instead of COUNT. COUNT counts the number of hits of a query. And the wildcard in Access-SQL is *, not '%'. Updated my answer one more time and hope it is correct now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.