I’m attaching a sample output file and my template so you can see me issue. I have a template with a static area of merge fields at the top of the document. I then have 2 areas (callbacks and relationships) both wrapped in mergefield with start and end areas. My issue is, the callbacks and relationships areas should only load for the records which are responsible for each person. Each area is loading all records in the table for each person. In the example I attached, each person has one relationship and one callback, but you can see it put them both on each.
Dim Query As String = "SELECT CASE WHEN ContactTxt = ‘’ THEN ‘-’ ELSE ContactTxt END AS ContactTxt, CASE WHEN TitleTxt = ‘’ THEN ‘-’ ELSE TitleTxt END AS TitleTxt, CASE WHEN CompanyTxt = ‘’ THEN ‘-’ ELSE CompanyTxt END AS CompanyTxt, CASE WHEN ContactKeyYNTxt = ‘’ THEN ‘-’ ELSE ContactKeyYNTxt END AS ContactKeyYNTxt, CASE WHEN ContactCategoryTxt = ‘’ THEN ‘-’ ELSE ContactCategoryTxt END AS ContactCategoryTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneDirectTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneDirectTxt = ‘’ THEN ‘-’ ELSE PhoneDirectTxt END end AS PhoneDirectTxt, PhoneDirectExtTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxDirectTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxDirectTxt = ‘’ THEN ‘-’ ELSE FaxDirectTxt END end AS FaxDirectTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneGeneralTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneGeneralTxt = ‘’ THEN ‘-’ ELSE PhoneGeneralTxt END end AS PhoneGeneralTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxGeneralTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxGeneralTxt = ‘’ THEN ‘-’ ELSE FaxGeneralTxt END end AS FaxGeneralTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneMobileTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneMobileTxt = ‘’ THEN ‘-’ ELSE PhoneMobileTxt END end AS PhoneMobileTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(PhoneHomeTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN PhoneHomeTxt = ‘’ THEN ‘-’ ELSE PhoneHomeTxt END end AS PhoneHomeTxt, case when len(ltrim(rtrim(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’))))=‘10’ then ‘’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),1,3)+’.’+’’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),4,3)+’.’+SUBSTRING(REPLACE(REPLACE(FaxHomeTxt,’-’,’’),’.’,’’),7,4) else CASE WHEN FaxHomeTxt = ‘’ THEN ‘-’ ELSE FaxHomeTxt END end AS FaxHomeTxt, CASE WHEN EmailURL = ‘’ THEN ‘-’ ELSE EmailURL END AS EmailURL, CASE WHEN REPLACE(WebURL,‘http://’,’’) = ‘’ THEN ‘-’ ELSE REPLACE(WebURL,‘http://’,’’) END AS WebURL, CASE WHEN Address1Txt > ‘’ THEN Address1Txt ELSE ‘-’ END AS Address1Txt, CASE WHEN Address2Txt > ‘’ THEN CHAR(10) + Address2Txt ELSE ‘’ END AS Address2Txt, CASE WHEN CityTxt > ‘’ THEN CHAR(10) + CityTxt ELSE ‘’ END AS CityTxt, CASE WHEN StateCode > ‘’ THEN StateCode ELSE ’ ’ END AS StateCode, CASE WHEN PostalCodeTxt > ‘’ THEN PostalCodeTxt ELSE ’ ’ END AS PostalCodeTxt, CASE WHEN CountryCode > ‘’ THEN CHAR(10) + CountryCode ELSE ‘’ END AS CountryCode, CASE WHEN ShippingAddress1Txt > ‘’ THEN ShippingAddress1Txt ELSE ‘-’ END AS ShippingAddress1Txt, CASE WHEN ShippingAddress2Txt > ‘’ THEN CHAR(10) + ShippingAddress2Txt ELSE ’ ’ END AS ShippingAddress2Txt, CASE WHEN ShippingCityTxt > ‘’ THEN CHAR(10) + ShippingCityTxt ELSE ’ ’ END AS ShippingCityTxt, CASE WHEN ShippingStateCode > ‘’ THEN ShippingStateCode ELSE ’ ’ END AS ShippingStateCode, CASE WHEN ShippingPostalCodeTxt > ‘’ THEN ShippingPostalCodeTxt ELSE ’ ’ END AS ShippingPostalCodeTxt, CASE WHEN ShippingCountryCode > ‘’ THEN CHAR(10) + ShippingCountryCode ELSE ’ ’ END AS ShippingCountryCode, ContactProfileMemo FROM ViewContact INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID WHERE ExportDetail.ExportID=’" & Request.QueryString("ID") & "’"
If qExportTemplateID = "24" Then
Dim doc As New Document("d:\hart11\templates\contactbasicprofile.doc")
Dim adapterTemplate1 As New SQLDataAdapter(Query, myConnection)
Dim dtTemplate1 As New DataTable()
adapterTemplate1.Fill(dtTemplate1)
doc.MailMerge.Execute(dtTemplate1)
doc.Save(SavePath)
ElseIf qExportTemplateID = "29" Then
Dim doc As New Document("d:\hart11\templates\contactdetailedprofile.doc")
'Callbacks
Dim adapterTemplate2 As New SQLDataAdapter("SELECT ActivityTxt, ActivityMemo, ActivityDueDate, ResponsibleContactTxt FROM ViewActivity INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewActivity.PrimaryRecordID WHERE ActivityStatusTxt <> ‘Terminated’ AND PrimaryRecordIDActiveYNTxt<>‘No’ AND (ActivityTypeTxt = ‘Callback’ OR ActivityTypeTxt = ‘Task’) AND ActivityActive <> ‘No’ AND ExportDetail.ExportID=’" & Request.QueryString("ID") & "’", myConnection)
Dim dtTemplate2 As New DataTable()
adapterTemplate2.Fill(dtTemplate2)
dtTemplate2.TableName = "Callbacks"
doc.MailMerge.ExecuteWithRegions(dtTemplate2)
'Relationships
Dim adapterTemplate3 As New SQLDataAdapter("SELECT Contact2ContactRelationshipTxt, InternalRelatedTo, InternalCompanyTxt, ContactContact2Memo FROM ViewRelationship INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewRelationship.ContactID WHERE ContactContact2ActiveYNTxt=‘Yes’ AND ExportDetail.ExportID=’" & Request.QueryString("ID") & "’", myConnection)
Dim dtTemplate3 As New DataTable()
adapterTemplate3.Fill(dtTemplate3)
dtTemplate3.TableName = "Relationships"
doc.MailMerge.ExecuteWithRegions(dtTemplate3)
Dim adapterTemplate1 As New SQLDataAdapter(Query, myConnection)
Dim dtTemplate1 As New DataTable()
adapterTemplate1.Fill(dtTemplate1)
doc.MailMerge.Execute(dtTemplate1)
doc.Save(SavePath)