Brian03
Brian03

Reputation: 23

MailMerge query string exceeds 255 characters

I'm having an issue with my automated mailmerge application. The application uses an MS Word template and an MS Word datasource. I'm using the following method to create the datasource and bind it to the template:

 expression.CreateDataSource(Name, PasswordDocument, WritePasswordDocument, HeaderRecord, MSQuery, SQLStatement, SQLStatement1, Connection, LinkToSource);

Here is the MSDN page on this method, for reference:

https://msdn.microsoft.com/en-us/library/office/ff820730(v=office.15).aspx

Everything works fine, however, some of my Word templates have many if statements, making the query string longer than 255 characters. When the application tries to execute the createDataSource method, it errors out.

It looks like the MSDN page says the SQLStatement, SQLStatement1 parameters can be used to break up the query into two portions. So, my question is how do utilize these parameters?? They are optional, and currently I am not using them.

Maybe there is even a better solution to this? Any insight would be great! I've done a lot web searching and haven't found a clear solution.

Please let me know if any additional info. is needed to better solve this.

Thanks!

Upvotes: 1

Views: 1110

Answers (1)

user1379931
user1379931

Reputation:

When Word executes the OpenDataSource method, it concatenates the SQLStatement and SQLStatement1 parameter values to create a single query statement. To use a simplified example, if your query needs to be

SELECT * FROM mytable

Then you could for example set

SQLStatement:="SELECT *", SQLStatement1:=" FROM mytable"

i.e. you need to ensure that significant spaces are in the concatenated string.

The limit on the total query length may vary depending on the data source. AFAICR some source types/connection methods still had a limit of 255 characters. But I think you'll get the full 511 that Word allows with a .mdb/.accdb source.

The other way to do it with a data source that supports queries/views may be to create a query or view within the database and use that as the data source instead.

Upvotes: 0

Related Questions