hazz_London
Registered User.
- Local time
- Today, 15:19
- Joined
- Sep 1, 2010
- Messages
- 23
Hi there.
I'm probably missing something really obvious, but I can't seem to get this query to work.
I'm building a VBA module in Word that pulls data from an Access database in order to automate a mail merge (the email addresses to be used are driven by the query). The code works fine, expect for one line of the query - !tbl001_ClientContactStatus.SecondaryMarketSup = True".
I have used this exact same query in Outlook to open a recordset and then pull off the list of email address and it worked fine. So I was surprised to find that it didn't work in the Word VBA environment.
If I take out the dodgy line (so there is just a WHERE on tbl001_ClientContactStatus.CompanyID = ...) it works fine. I have tried many variations, including just one WHERE on tbl001_ClientContactStatus.SecondaryMarketSup = True, with quotation marks etc. but no joy.
Could it be down to the order? Any help would be hugely appreciated.
I have included below an example debug.print of the SQL statement, as well as the VBA code that builds it.
Thanks again for any help.
[Debug.Print]
SELECT tbl001_ClientContactStatus.SecondaryMarketSup,tbl001_Contact.EmailAddress1 FROM tbl001_Contact INNER JOIN tbl001_ClientContactStatus ON tbl001_ClientContactStatus.ContactID = tbl001_Contact.ContactID WHERE tbl001_ClientContactStatus.CompanyID =47AND tbl001_ClientContactStatus.SecondaryMarketSup = True;
[VBA code]
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim strSQL As String
Dim MyClientID As String
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.ActiveDocument
MyClientID = 47
strSQL = "SELECT tbl001_ClientContactStatus.SecondaryMarketSup,tbl001_Contact.EmailAddress1 "
strSQL = strSQL & "FROM tbl001_Contact "
strSQL = strSQL & "INNER JOIN tbl001_ClientContactStatus "
strSQL = strSQL & "ON tbl001_ClientContactStatus.ContactID = tbl001_Contact.ContactID "
strSQL = strSQL & "WHERE tbl001_ClientContactStatus.CompanyID =" & MyClientID
strSQL = strSQL & "AND tbl001_ClientContactStatus.SecondaryMarketSup = True"
strSQL = strSQL & ";"
Debug.Print strSQL
wdDoc.MailMerge.OpenDataSource _
Name:="Y:\MAINFOLDER\SH_Management_BE.mdb", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="Data", _
SQLStatement:=strSQL
wdDoc.MailMerge.Destination = wdSendToEmail
'wdDoc.MailMerge.Execute
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub
I'm probably missing something really obvious, but I can't seem to get this query to work.
I'm building a VBA module in Word that pulls data from an Access database in order to automate a mail merge (the email addresses to be used are driven by the query). The code works fine, expect for one line of the query - !tbl001_ClientContactStatus.SecondaryMarketSup = True".
I have used this exact same query in Outlook to open a recordset and then pull off the list of email address and it worked fine. So I was surprised to find that it didn't work in the Word VBA environment.
If I take out the dodgy line (so there is just a WHERE on tbl001_ClientContactStatus.CompanyID = ...) it works fine. I have tried many variations, including just one WHERE on tbl001_ClientContactStatus.SecondaryMarketSup = True, with quotation marks etc. but no joy.
Could it be down to the order? Any help would be hugely appreciated.
I have included below an example debug.print of the SQL statement, as well as the VBA code that builds it.
Thanks again for any help.
[Debug.Print]
SELECT tbl001_ClientContactStatus.SecondaryMarketSup,tbl001_Contact.EmailAddress1 FROM tbl001_Contact INNER JOIN tbl001_ClientContactStatus ON tbl001_ClientContactStatus.ContactID = tbl001_Contact.ContactID WHERE tbl001_ClientContactStatus.CompanyID =47AND tbl001_ClientContactStatus.SecondaryMarketSup = True;
[VBA code]
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim strSQL As String
Dim MyClientID As String
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.ActiveDocument
MyClientID = 47
strSQL = "SELECT tbl001_ClientContactStatus.SecondaryMarketSup,tbl001_Contact.EmailAddress1 "
strSQL = strSQL & "FROM tbl001_Contact "
strSQL = strSQL & "INNER JOIN tbl001_ClientContactStatus "
strSQL = strSQL & "ON tbl001_ClientContactStatus.ContactID = tbl001_Contact.ContactID "
strSQL = strSQL & "WHERE tbl001_ClientContactStatus.CompanyID =" & MyClientID
strSQL = strSQL & "AND tbl001_ClientContactStatus.SecondaryMarketSup = True"
strSQL = strSQL & ";"
Debug.Print strSQL
wdDoc.MailMerge.OpenDataSource _
Name:="Y:\MAINFOLDER\SH_Management_BE.mdb", _
LinkToSource:=True, AddToRecentFiles:=False, _
Connection:="Data", _
SQLStatement:=strSQL
wdDoc.MailMerge.Destination = wdSendToEmail
'wdDoc.MailMerge.Execute
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub