Question

I have the problem related to word automation \ mailmerge, QueryString command and SQL sentence in Word 2007.

If I use select and "=" operator, everything work as expected.

doc.MailMerge.DataSource.QueryString =
"SELECT * FROM \"TableName\" WHERE \"Id\" = 10";

If I use select and "LIKE" operator, it does not:

doc.MailMerge.DataSource.QueryString =
"SELECT * FROM \"TableName\" WHERE \"EmailAddress\" LIKE '%com%'";

Looks like Word do not understand "LIKE" operator and just skip it. It returns ALL the records from the table.

Is there any way to use "like" in the querystring?

UPDATE: actually, MailMerge.Execute works correctly with LIKE statement. The problem is a bit different. There is place in the system where we show RecordCount (doc.MailMerge.DataSource.RecordCount), and it returns incorrect value (it returns count of the all records in the table). So looks like Word can't determine count of records if LIKE statement is using in QueryString.

Was it helpful?

Solution

What you can do depends on the data source.

If it's a recognised Word document type (.doc, .rtf, .docx etc.) or something that Word opens via a text file converter, Word uses a very simple internal dialect of SQL that only lets you define the fields you want, simple WHERE conditions, and a simple ORDER BY. (i.e., roughly what you can do in the Sort/Filter dialog). You don't get "LIKE"

If it's a Jet data source, such as Access or Excel, you get the Jet dialect of SQL, and either the Jet wildcards (? and *) or the SQL standard wildcards (_ and %) depending on the source and how you are connecting. Typically, you should get LIKE, and you should be able to use _ and %, but not for some databases connected via DDE.

For everything else such as SQL Server, Oracle etc., in theory you get the SQL dialect appropriate for that server. But the bit of code Word uses to process OLE DB queries (the Office Data Source Object, ODSO) can mess around with the query string before sending it.

Some notes consolidated from comments:

The "Contains" options in Word's Sort/Filter dialog box results in a LIKE clause in the SQL. For SQL Server, the SQL clause is like this:

"LIKE N'%com%'"

(notice the "N").

A simple example filters correctly here (Word 2010+SQL Server 2008 R2, with the default OLE DB connection and the old SQLOLEDB.1 provider. There would be a problem if you are connecting via ODBC and the column is a Unicode column (NVARCHAR etc.) but then you would not see any data in Unicode columns. There might be a problem if the column type is a long type such as a memo and the % needs to match 128 chars or some such - can't remember what the rules are for SQL Server in that case.

The SQL Server Profiler can be a useful server-side tool for investigating unexpected query results. In this case the OP discovered that the query was executing as expected but that the RecordCount (doc.MailMerge.DataSource.RecordCount) returns the count of all the records in the table, rather than the number of records retrieved.

Whether Recordcount returns the correct value or not depends on the OLE DB provider and/or its settings. You may see VBA sample code that tries hard to test for "last record" rather than rely on the count, for this reason.

Personally, if necessary I would consider executing a separate query to get the count(*) but of course there is then the possibility that the actual record count can change when you do the retrieval. (FWIW I doubt you can use SQL transactions to help in this scenario, but it might be worth a try).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top