Need help with QueryString in Mail Merge

Calling all Mail Merge masters!

The following code is VB code I'm using to conduct a Mail Merge.

The code is running on XP under MSO 03 (Word VBA6)

This code simply opens the template file (if it is not already open) and sets up the merge properties and runs the merge.

The extra piece you see, in bold, in the .DataSource.QueryString is my attempt to get the code to filter the contents of the data file to just those entries who have 0 for their Owners value.

This block of code you see below can run from 0 to 16 contacts so I need the Mail Merge to edit the data source before performing the merge.

The part in bold is my problem. It continues to give me a 4198 run-time error and simply says 'Command Failed'.

[code]
Sub MergeTest()

Err.Number = 0
On Error GoTo ErrorHandler
Windows("New Ltr-C01.doc").Activate
ErrorHandler:
If Err.Number = 5941 Then
Documents.Open ("X:\Letters\English\Templates\New\New Ltr-C01.doc")
End If
Set letterfile = Documents("New Ltr-C01.doc")

sourcefile = "X:Letters\Data\Test Source File.xls"
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=sourcefile;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:"
' connectionstring = "Merge"
' connectionstring = ""
letterfile.MailMerge.MainDocumentType = wdFormLetters
letterfile.MailMerge.OpenDataSource Name:=sourcefile, _
ConfirmConversions:=False, _
ReadOnly:=False, _
LinkToSource:=True, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:=connectionstring, _
SQLStatement:="SELECT * FROM `Merge$`", _
SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.DataSource.QueryString = "SELECT * FROM `Merge$` WHERE `Owners`=`0`" & ""
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End Sub
[/code]

The 1st connectionstring variable above was given to me when I recorded the Mail merge. However, the recording would not record any code about the filtering process.

I have tried a variety of ways to state the part in bold:

[code]
.DataSource.QueryString = "SELECT * FROM `Merge$` WHERE Owners=0"
.DataSource.QueryString = "SELECT * FROM Merge WHERE `Owners`=`0`" & ""
.DataSource.QueryString = "SELECT * FROM `Merge` WHERE `Owners`=`0`" & ""
.DataSource.QueryString = "SELECT * FROM `Merge$` WHERE ((Owners=0))" & ""
[/code]

So far my search for answers has proved unfruitful, anyone have any ideas?

Thanks! JT

[3037 byte] By [JediTiger] at [2007-12-16]
# 1
maybe this will help... rim is an integer and everything else is a string
from an insert startment I wrote that build the string using variables in the program


Dim sqlComm3 As New Data.SqlClient.SqlCommand("insert into dbo.ACRMLog values ( " & rim & ",'" & date1 & "','" & type1 & "','" & emp & "','" & desc & "');", SqlConn)

notice rim (an int) is surrounded by the " & rim & "
DOUBLE QUOTE & rim & DOUBLE QUOTE

the above is for an int!

for a string it's '" & string & "'

that's SINGLE QUOTE DOUBLE QUOTE & string & DOUBLE QUOTE SINGLE QUOTE

Does that help at all?

Carl

Zep-- at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
JT,

Have been struggling with exactly the same problem, I got my query working by replacing the ` by brackets [] .. so:

.DataSource.QueryString = "SELECT * FROM [Merge$] WHERE [Owners]='0'"

I put the ' around the 0 in your query, assuming the data is text, otherwise these should go off course.

HTH,

Dave

DaveBos at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
You would only put the ' ' in for non numerical entries.

the [] was needed because the table name Merge$ requires the [] because of the $ symbol.

the query string, should be
.DataSource.QueryString = "SELECT * FROM [Merge$] WHERE Owners = 0"

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...