Microsoft Access - Convert an update inner join group by count to Access VBA

Asked By Charles Bostic on 29-Jun-12 01:43 PM
I created a query that counts values in a field of one table and updates them into a designated column of a table linked by common Org and contact IDs. The issue I'm having is converting this SQL coding to proper VBA code. This is how the code looks in SQL: 

SELECT US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Org, US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Person, Count(dbo_CRM_ACTIVITY.activity_id) AS CountOfactivity_id INTO act_count
FROM US_CA_Contacts_v001_CB1 INNER JOIN dbo_CRM_ACTIVITY ON (US_CA_Contacts_v001_CB1.co_id = dbo_CRM_ACTIVITY.co_crm_id) AND (US_CA_Contacts_v001_CB1.ct_id = dbo_CRM_ACTIVITY.ct_crm_id)
GROUP BY US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Org, US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Person;

I've created several functions but they all recieve some type of error.
When you get a chance, I'd appreciate it.
wally eye replied to Charles Bostic on 29-Jun-12 06:57 PM
I normally split it into small chunks per line:

dim dbCurr      as dao.database
dim qdfAction    as querydef

dim strSQL       as string

strsql = "SELECT US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Org, " _
  & "US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Person, " _
  & "Count(dbo_CRM_ACTIVITY.activity_id) AS CountOfActivity_id " _
  & "INTO act_count " _
  & "FROM US_CA_Contacts_v001_CB1 " _
  & "INNER JOIN dbo_CRM_ACTIVITY " _
  & "ON (US_CA_Contacts_v001_CB1.co_id = dbo_CRM_ACTIVITY.co_crm_id) " _
  & "AND (US_CA_Contacts_v001_CB1.ct_id = dbo_CRM_ACTIVITY.ct_crm_id) " _
  & "GROUP BY US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Org, " _ 
  & "US_CA_Contacts_v001_CB1.Business_Partner_Number_for_Person;"


set dbcurr = currentdb
Set qdfAction = dbCurr.CreateQueryDef("", strSQL)
qdfAction.Execute

dbcurr.close

set qdfaction = nothing
set dbcurr = nothing

Assuming your syntax works in Access, this should do it for you.  I don't think I've used the Into keyword before, is that where your question is?
Charles Bostic replied to wally eye on 05-Jul-12 09:02 PM
Thanks, it works