I
irobot8
I'm building a contacts database in VB, with an Access back end.
WRT table structure, each contact undertakes many activities. There
are 3 main tables:
1. tbContacts (stores contact details e.g. Name, Address, Tel No etc.)
2. tbActivityBridge (which details the activities each contact has
undertaken, containing Activity ID and Contact ID only)
3. tbActivities (with details of each individual activity e.g. Code,
Description, Location etc.)
I'm providing the user with a form within the VB front-end which
enables them to choose up to 5 activity codes, in order that they can
produce labels in MS Word for contacts that have undertaken specified
activities. The VB I've used is:
MyMailMerge.OpenDataSource Name:=strConnection, _
LinkToSource:=True, SQLStatement:=strQuery,
SQLStatement1:=strCriteria, _
SubType:=wdMergeSubTypeWord2000
The SQL string I've used for strCriteria is dynamically generated
according to what the user enters in the VB form for activity codes.
The strQuery string is fixed, but overruns the 255 character limit
imposed by Word. The SQL is as follows:
strQuery = "SELECT tbContacts.Contact_Title,
tbContacts.Contact_First_Name, tbContacts.Contact_Surname,
tbContacts.Contact_Address_1, tbContacts.Contact_Address_2,
tbContacts.Contact_Town, tbContacts.Contact_County_ID,
tbContacts.Contact_Postcode
FROM tbContacts INNER JOIN (tbActivities INNER JOIN tbActivityBridge
ON tbActivities.Activity_ID = tbActivityBridge.Activity_ID) ON
tbContacts.Contact_ID = tbActivityBridge.Contact_ID
GROUP BY tbContacts.Contact_Title, tbContacts.Contact_First_Name,
tbContacts.Contact_Surname, tbContacts.Contact_Address_1,
tbContacts.Contact_Address_2, tbContacts.Contact_Town,
tbContacts.Contact_County_ID, tbContacts.Contact_Postcode "
strCriteria = "HAVING (((Min([tbActivities].[Activity_Code]) Like '*"
& strActivityCode1 "*')=True)) AND
(((Min([tbActivities].[Activity_Code]) Like '*" & strActivityCode2 &
"*')=True));"
The strQuery string is over 600 characters!!
Can anyone suggest a way of reducing this, so that I can use the above
method with sqlStatement and sqlStatement1. Or perhaps there is
another way using queryDefs, stored queries, sub-queries et al.
If I use * within the select statement, I get a 'Cannot have aggregate
function in WHERE clause' errors, or 'You tried to execute a query
that does not include the specified expression Contact_ID as part of
an aggregate function'.
Any help greatly appreciated.
irobot8
WRT table structure, each contact undertakes many activities. There
are 3 main tables:
1. tbContacts (stores contact details e.g. Name, Address, Tel No etc.)
2. tbActivityBridge (which details the activities each contact has
undertaken, containing Activity ID and Contact ID only)
3. tbActivities (with details of each individual activity e.g. Code,
Description, Location etc.)
I'm providing the user with a form within the VB front-end which
enables them to choose up to 5 activity codes, in order that they can
produce labels in MS Word for contacts that have undertaken specified
activities. The VB I've used is:
MyMailMerge.OpenDataSource Name:=strConnection, _
LinkToSource:=True, SQLStatement:=strQuery,
SQLStatement1:=strCriteria, _
SubType:=wdMergeSubTypeWord2000
The SQL string I've used for strCriteria is dynamically generated
according to what the user enters in the VB form for activity codes.
The strQuery string is fixed, but overruns the 255 character limit
imposed by Word. The SQL is as follows:
strQuery = "SELECT tbContacts.Contact_Title,
tbContacts.Contact_First_Name, tbContacts.Contact_Surname,
tbContacts.Contact_Address_1, tbContacts.Contact_Address_2,
tbContacts.Contact_Town, tbContacts.Contact_County_ID,
tbContacts.Contact_Postcode
FROM tbContacts INNER JOIN (tbActivities INNER JOIN tbActivityBridge
ON tbActivities.Activity_ID = tbActivityBridge.Activity_ID) ON
tbContacts.Contact_ID = tbActivityBridge.Contact_ID
GROUP BY tbContacts.Contact_Title, tbContacts.Contact_First_Name,
tbContacts.Contact_Surname, tbContacts.Contact_Address_1,
tbContacts.Contact_Address_2, tbContacts.Contact_Town,
tbContacts.Contact_County_ID, tbContacts.Contact_Postcode "
strCriteria = "HAVING (((Min([tbActivities].[Activity_Code]) Like '*"
& strActivityCode1 "*')=True)) AND
(((Min([tbActivities].[Activity_Code]) Like '*" & strActivityCode2 &
"*')=True));"
The strQuery string is over 600 characters!!
Can anyone suggest a way of reducing this, so that I can use the above
method with sqlStatement and sqlStatement1. Or perhaps there is
another way using queryDefs, stored queries, sub-queries et al.
If I use * within the select statement, I get a 'Cannot have aggregate
function in WHERE clause' errors, or 'You tried to execute a query
that does not include the specified expression Contact_ID as part of
an aggregate function'.
Any help greatly appreciated.
irobot8