query with join for VBA mail merge (1 Viewer)

NT100

Registered User.
Local time
Today, 13:21
Joined
Jul 29, 2017
Messages
148
I use MS ACCESS vba to do mailmerge word documents with the following queries (qryTReapptNomination_HCAP) and (qryTNewNomination_HCAP) separately

Run frmReport
Re-appointment - > Honorary Clinical Assistant Professor
It works fine with qryTReapptNomination_HCAP

Its query structure is shown bleow
SELECT tblProcessTReAppt.TRef, tblTutor.UID, tblTutor.Title, tblTutor.LastName, tblTutor.FirstName, tblTutor.Alias, tblTutor.ClinicAdd1, tblTutor.ClinicAdd2, tblTutor.ClinicAdd3, tblTRank.HDesc, tblTAppt.JobPosition, tblTAppt.Company, tblTAppt.Honorarium, tblTAppt.Allowance, tblTAppt.FCF_No, tblTAppt.budget
FROM ((tblProcessTReAppt LEFT JOIN tblTutor ON tblProcessTReAppt.TRef = tblTutor.TRef) LEFT JOIN tblTAppt ON tblProcessTReAppt.TRef = tblTAppt.TRef) LEFT JOIN tblTRank ON tblTAppt.RankCode = tblTRank.ID
WHERE (tblTAppt.AppointedBy = "UG") and (tblTAppt.RankCode=1) and (tblProcessTReappt.ReplyToInvitation= "Y")
ORDER BY tblTutor.LastName, tblTutor.FirstName, tblTutor.Alias;

However, run frmReport
New Appointment - > Honorary Clinical Assistant Professor
pops up of “Confirm Data Source” and then runs into error "5922: Word was unable to open the data source".

qryTNewNomination_HCAP
SELECT PT.TRef, PT.ApptDtStart, PT.ApptDtEnd, T.UID, T.Title, T.LastName, T.FirstName, T.Alias, T.EMail_1, T.BQual, T.PQual1, T.PQual2, T.PQual3, T.ClinicAdd1, T.ClinicAdd2, T.ClinicAdd3, TR.HDesc, NA.JobPosition, NA.Company, NA.Honorarium, NA.Allowance, NA.FCF_No, NA.budget
FROM ((tblProcessTNewAppt AS PT INNER JOIN tblTutor AS T ON PT.TRef = T.TRef) INNER JOIN tblTNewAppt AS NA ON PT.TRef = NA.TRef) INNER JOIN tblTRank AS TR ON NA.RankCode = TR.ID
WHERE (PT.ReplyToInvitation= "Y") AND (NA.RankCode = 1)
ORDER BY T.LastName, T.FirstName, T.Alias;

I have inspected several times and found nothing wrong with the structures and queries. It's weird that one query works fine but a similiar query doesn't work for the same VBA codes.

Pls. find the attached application for details.

Thank you in advance for any idea on this problem.
 

Attachments

  • Appt.zip
    139.7 KB · Views: 247

NT100

Registered User.
Local time
Today, 13:21
Joined
Jul 29, 2017
Messages
148
I use MS ACCESS vba to do mailmerge word documents with the following queries (qryTReapptNomination_HCAP) and (qryTNewNomination_HCAP) separately

Run frmReport
Re-appointment - > Honorary Clinical Assistant Professor
It works fine with qryTReapptNomination_HCAP

Its query structure is shown bleow
SELECT tblProcessTReAppt.TRef, tblTutor.UID, tblTutor.Title, tblTutor.LastName, tblTutor.FirstName, tblTutor.Alias, tblTutor.ClinicAdd1, tblTutor.ClinicAdd2, tblTutor.ClinicAdd3, tblTRank.HDesc, tblTAppt.JobPosition, tblTAppt.Company, tblTAppt.Honorarium, tblTAppt.Allowance, tblTAppt.FCF_No, tblTAppt.budget
FROM ((tblProcessTReAppt LEFT JOIN tblTutor ON tblProcessTReAppt.TRef = tblTutor.TRef) LEFT JOIN tblTAppt ON tblProcessTReAppt.TRef = tblTAppt.TRef) LEFT JOIN tblTRank ON tblTAppt.RankCode = tblTRank.ID
WHERE (tblTAppt.AppointedBy = "UG") and (tblTAppt.RankCode=1) and (tblProcessTReappt.ReplyToInvitation= "Y")
ORDER BY tblTutor.LastName, tblTutor.FirstName, tblTutor.Alias;

However, run frmReport
New Appointment - > Honorary Clinical Assistant Professor
pops up of “Confirm Data Source” and then runs into error "5922: Word was unable to open the data source".

qryTNewNomination_HCAP
SELECT PT.TRef, PT.ApptDtStart, PT.ApptDtEnd, T.UID, T.Title, T.LastName, T.FirstName, T.Alias, T.EMail_1, T.BQual, T.PQual1, T.PQual2, T.PQual3, T.ClinicAdd1, T.ClinicAdd2, T.ClinicAdd3, TR.HDesc, NA.JobPosition, NA.Company, NA.Honorarium, NA.Allowance, NA.FCF_No, NA.budget
FROM ((tblProcessTNewAppt AS PT INNER JOIN tblTutor AS T ON PT.TRef = T.TRef) INNER JOIN tblTNewAppt AS NA ON PT.TRef = NA.TRef) INNER JOIN tblTRank AS TR ON NA.RankCode = TR.ID
WHERE (PT.ReplyToInvitation= "Y") AND (NA.RankCode = 1)
ORDER BY T.LastName, T.FirstName, T.Alias;

I have inspected several times and found nothing wrong with the structures and queries. It's weird that one query works fine but a similiar query doesn't work for the same VBA codes.

Pls. find the attached application for details.

Thank you in advance for any idea on this problem.

Does anybody have suggestions on this weird problem? I've updated my application as attached.

Thank you in advance.
 

Attachments

  • Appt_1.zip
    161.7 KB · Views: 153

bastanu

AWF VIP
Local time
Yesterday, 22:21
Joined
Apr 13, 2010
Messages
1,402
I don't think is your query, I believe the problem is with your template. Can you try to recreate the Word template for the new appointments and see if you still get an error?

Cheers,
Vlad
 

NT100

Registered User.
Local time
Today, 13:21
Joined
Jul 29, 2017
Messages
148
I don't think is your query, I believe the problem is with your template. Can you try to recreate the Word template for the new appointments and see if you still get an error?

Cheers,
Vlad

Thank you for your suggestion.

I've created a new document for it. The result is the same that I cannot mailmerge with that query from VBA.
 

bastanu

AWF VIP
Local time
Yesterday, 22:21
Joined
Apr 13, 2010
Messages
1,402
Hi,

Please have a look at the attached files. I have created a new query using the actual tables instead of aliases, changed the mail-merge code to point to it and created a new Word template (started with a new doc and copied your table with the mail merge fields). You will also see a new button (Setup Mail Merge) where I used some old code I had to open a Word doc for setting up the merge.

Long story short, seems to be working now...

Enjoy!
Cheers,
Vlad
 

Attachments

  • Appt_Updated.zip
    213.2 KB · Views: 238

NT100

Registered User.
Local time
Today, 13:21
Joined
Jul 29, 2017
Messages
148
Yours works fine. However, when I copied your mail merge document "Nomination Form.docx" to another name like "Nomination_Form_Re-Appointment_HCT" and applied the same query. It pops up "Confirm Data Source" again.

What's wrong?
 

bastanu

AWF VIP
Local time
Yesterday, 22:21
Joined
Apr 13, 2010
Messages
1,402
Not sure what happens unless I see it. But I don't generally like to use Access queries as the mail merger source as they open and lock the file if you open the Word documents. I have modified your sample db to use the method I've been using for many years now; I am exporting your queries as comma delimited text files and then use the resultant text file as the mail-merge source.

Give it a try and please post back the results.

Cheers,
Vlad
 

Attachments

  • Appointment_UpdatedByVlad_March7_2018.zip
    98.3 KB · Views: 237

moke123

AWF VIP
Local time
Today, 01:21
Joined
Jan 11, 2013
Messages
3,920
Another option for you may be Albert Kallal's Super Easy Mail Merge. Its very flexible and easy to adapt to any situation.

Read through Albert's comments in the wordcode module to see the many ways it can be used.

I use it in an unconventional way as I have nearly 100 merge fields. I use a temp database to hold a temp table which is far from normalized. I use a template Table as a source for the temp table in the temp database. this allows me to easily add more merge fields as the project changes. I use a series of insert and update procedures to add data to the temp table. I also use Document libraries to separate the word templates into different categories.
heres a quick sample db
 

Attachments

  • NewWordDemo.zip
    207.2 KB · Views: 219

NT100

Registered User.
Local time
Today, 13:21
Joined
Jul 29, 2017
Messages
148
Thank you for the suggestions. I'll study them.

On the other hand, is the mail merge VBA a MS ACCESS software error?

Should Microsoft take immediate appropriate actions for remedies instead of letting its customers working around the problem for alternative solutions?
 

Users who are viewing this thread

Top Bottom